import java.sql.Timestamp; |
002 |
003 | import java.text.SimpleDateFormat; |
004 |
005 | import java.util.Date; |
006 |
007 | import java.util.GregorianCalendar; |
008 |
009 | |
010 |
011 | public class DateTest { |
012 |
013 | |
014 |
015 | public static void main(String[] args) { |
016 |
017 | |
018 |
019 | SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" ); |
020 |
021 | DateTest test = new DateTest(); |
022 |
023 | //Date |
024 |
025 | Date currentDate = new Date(); |
026 |
027 | System.out.println( "当前日期是:" + df.format(currentDate)); |
028 |
029 | System.out.println( "一周后的日期是:" + df.format(test.nextWeek(currentDate))); |
030 |
031 | System.out.println( "一月后的日期是:" + df.format(test.nextMonth(currentDate))); |
032 |
033 | System.out.println( "一年后的日期是:" + df.format(test.nextYear(currentDate))); |
034 |
035 | //Timestamp |
036 |
037 | Timestamp currentTime = new Timestamp(System.currentTimeMillis()); |
038 |
039 | System.out.println( "当前日期是:" + df.format(currentTime)); |
040 |
041 | System.out.println( "一周后的日期是:" + df.format(test.nextWeek(currentTime))); |
042 |
043 | System.out.println( "一月后的日期是:" + df.format(test.nextMonth(currentTime))); |
044 |
045 | System.out.println( "一年后的日期是:" + df.format(test.nextYear(currentTime))); |
046 |
047 | |
048 |
049 | //另一种计算方式,这种方式计算月和年的日期比较困难 |
050 |
051 | Timestamp nextTime = new Timestamp(currentTime.getTime() + 7 * 24 * 60 * 60 * 1000 ); |
052 |
053 | System.out.println( "当前日期是:" + df.format(currentTime)); |
054 |
055 | System.out.println( "一周后的日期是:" + df.format(nextTime)); |
056 |
057 | |
058 |
059 | } |
060 |
061 | |
062 |
063 | //获取下一周的日期 |
064 |
065 | public Date nextWeek(Date currentDate) { |
066 |
067 | GregorianCalendar cal = new GregorianCalendar(); |
068 |
069 | cal.setTime(currentDate); |
070 |
071 | cal.add(GregorianCalendar.DATE, 7 ); //在日期上加7天 |
072 |
073 | return cal.getTime(); |
074 |
075 | } |
076 |
077 | |
078 |
079 | //获取本周日的日期 |
080 |
081 | public Date getSunday(Date monday) { |
082 |
083 | GregorianCalendar cal = new GregorianCalendar(); |
084 |
085 | cal.setTime(monday); |
086 |
087 | cal.add(GregorianCalendar.DATE, 6 ); //在日期上加6天 |
088 |
089 | return cal.getTime(); |
090 |
091 | } |
092 |
093 | |
094 |
095 | //获取下一月的日期 |
096 |
097 | public Date nextMonth(Date currentDate) { |
098 |
099 | GregorianCalendar cal = new GregorianCalendar(); |
100 |
101 | cal.setTime(currentDate); |
102 |
103 | cal.add(GregorianCalendar.MONTH, 1 ); //在月份上加1 |
104 |
105 | return cal.getTime(); |
106 |
107 | } |
108 |
109 | |
110 |
111 | //获取下一年的日期 |
112 |
113 | public Date nextYear(Date currentDate) { |
114 |
115 | GregorianCalendar cal = new GregorianCalendar(); |
116 |
117 | cal.setTime(currentDate); |
118 |
119 | cal.add(GregorianCalendar.YEAR, 1 ); //在年上加1 |
120 |
121 | return cal.getTime(); |
122 |
123 | } |
124 |
125 | //获取当前季度 |
126 | public int getQuarter(){ |
127 | Calendar cal = Calendar.getInstance(); |
128 | int month = cal.get(Calendar.MONTH); |
129 | return month/ 3 ; |
130 | } |
131 | } |