001/* 002 * Copyright (c) 2012, 2018, Anatole Tresch, Werner Keil and others by the @author tag. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016package org.javamoney.moneta.convert; 017 018import java.io.Serializable; 019import java.util.Calendar; 020import java.util.GregorianCalendar; 021 022/** 023 * Class to model a local date without timezone info. 024 */ 025public final class LocalDate implements Comparable<LocalDate>, Serializable{ 026 /** 027 * 028 */ 029 private static final long serialVersionUID = 8427707792648468834L; 030 final int year; 031 final int month; 032 final int dayOfMonth; 033 034 LocalDate(int year, int month, int dayOfMonth){ 035 this.year = year; 036 this.month = month; 037 this.dayOfMonth = dayOfMonth; 038 } 039 040 static LocalDate now(){ 041 Calendar cal = GregorianCalendar.getInstance(); 042 return from(cal); 043 } 044 045 static LocalDate yesterday(){ 046 return beforeDays(1); 047 } 048 049 static LocalDate beforeDays(int days){ 050 Calendar cal = GregorianCalendar.getInstance(); 051 cal.add(Calendar.DAY_OF_YEAR, days*-1); 052 return from(cal); 053 } 054 055 public LocalDate minusDays(int days){ 056 Calendar cal = toCalendar(); 057 cal.add(Calendar.DAY_OF_YEAR, days*-1); 058 return from(cal); 059 } 060 061 /** 062 * Create a new (local/default Locale based) GregorianCalendar instance. 063 * @return a new (local/default Locale based) GregorianCalendar instance, not null. 064 */ 065 public Calendar toCalendar() { 066 return new GregorianCalendar(year, month-1, dayOfMonth); 067 } 068 069 /** 070 * Cerates a new instance from the given Calendar. 071 * @param cal the Calendar, not null. 072 * @return the corresponding LocalDate instance, never null. 073 */ 074 public static LocalDate from(Calendar cal) { 075 int year = cal.get(Calendar.YEAR); 076 int month = cal.get(Calendar.MONTH)+1; 077 int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); 078 return new LocalDate(year, month, dayOfMonth); 079 } 080 081 public int getYear() { 082 return year; 083 } 084 085 public int getMonth() { 086 return month; 087 } 088 089 public int getDayOfMonth() { 090 return dayOfMonth; 091 } 092 093 public boolean before(LocalDate localDate){ 094 return compareTo(localDate)<0; 095 } 096 097 public boolean after(LocalDate localDate){ 098 return compareTo(localDate)>0; 099 } 100 101 @Override 102 public int compareTo(LocalDate o) { 103 return 0; 104 } 105 106 @Override 107 public boolean equals(Object o) { 108 if (this == o) return true; 109 if (!(o instanceof LocalDate)) return false; 110 111 LocalDate localDate = (LocalDate) o; 112 113 return dayOfMonth == localDate.dayOfMonth && month == localDate.month && year == localDate.year; 114 115 } 116 117 @Override 118 public int hashCode() { 119 int result = year; 120 result = 31 * result + month; 121 result = 31 * result + dayOfMonth; 122 return result; 123 } 124 125 @Override 126 public String toString() { 127 return "LocalDate{" + 128 "year=" + year + 129 ", month=" + month + 130 ", dayOfMonth=" + dayOfMonth + 131 '}'; 132 } 133 134 135}