1 27 28 29 package com.sun.ebank.util; 30 31 import java.util.*; 32 import java.text.SimpleDateFormat ; 33 34 35 39 public final class DateHelper { 40 public static final Date getDate(int year, int month, int day, int hour, 41 int minute) { 42 Calendar cal = 44 new GregorianCalendar(year, intToCalendarMonth(month), day, hour, 45 minute); 46 47 return cal.getTime(); 48 } 49 51 public static final Date getDate(int year, int month, int day) { 52 Calendar cal = 55 new GregorianCalendar(year, intToCalendarMonth(month), day); 56 57 return cal.getTime(); 58 } 59 61 static public final Date addDays(Date target, int days) { 62 long msPerDay = 1000 * 60 * 60 * 24; 67 long msTarget = target.getTime(); 68 long msSum = msTarget + (msPerDay * days); 69 Date result = new Date(); 70 result.setTime(msSum); 71 72 return result; 73 } 74 76 static public int dayDiff(Date first, Date second) { 77 long msPerDay = 1000 * 60 * 60 * 24; 80 long diff = 81 (first.getTime() / msPerDay) - (second.getTime() / msPerDay); 82 Long convertLong = new Long (diff); 83 84 return convertLong.intValue(); 85 } 86 88 static public int getYear(Date date) { 89 Calendar cal = new GregorianCalendar(); 90 cal.setTime(date); 91 92 return cal.get(Calendar.YEAR); 93 } 94 96 static public int getMonth(Date date) { 97 Calendar cal = new GregorianCalendar(); 98 cal.setTime(date); 99 100 int calendarMonth = cal.get(Calendar.MONTH); 101 102 return calendarMonthToInt(calendarMonth); 103 } 104 106 static public int getDay(Date date) { 107 Calendar cal = new GregorianCalendar(); 108 cal.setTime(date); 109 110 return cal.get(Calendar.DAY_OF_MONTH); 111 } 112 114 static public int getHour(Date date) { 115 Calendar cal = new GregorianCalendar(); 116 cal.setTime(date); 117 118 return cal.get(Calendar.HOUR_OF_DAY); 119 } 120 122 static public int getMinute(Date date) { 123 Calendar cal = new GregorianCalendar(); 124 cal.setTime(date); 125 126 return cal.get(Calendar.MINUTE); 127 } 128 130 private static int calendarMonthToInt(int calendarMonth) { 131 if (calendarMonth == Calendar.JANUARY) { 132 return 1; 133 } else if (calendarMonth == Calendar.FEBRUARY) { 134 return 2; 135 } else if (calendarMonth == Calendar.MARCH) { 136 return 3; 137 } else if (calendarMonth == Calendar.APRIL) { 138 return 4; 139 } else if (calendarMonth == Calendar.MAY) { 140 return 5; 141 } else if (calendarMonth == Calendar.JUNE) { 142 return 6; 143 } else if (calendarMonth == Calendar.JULY) { 144 return 7; 145 } else if (calendarMonth == Calendar.AUGUST) { 146 return 8; 147 } else if (calendarMonth == Calendar.SEPTEMBER) { 148 return 9; 149 } else if (calendarMonth == Calendar.OCTOBER) { 150 return 10; 151 } else if (calendarMonth == Calendar.NOVEMBER) { 152 return 11; 153 } else if (calendarMonth == Calendar.DECEMBER) { 154 return 12; 155 } else { 156 return 1; 157 } 158 } 159 161 public static String format(Date date, String pattern) { 162 SimpleDateFormat formatter = new SimpleDateFormat (pattern); 167 168 return formatter.format(date); 169 } 170 172 private static int intToCalendarMonth(int month) { 173 if (month == 1) { 174 return Calendar.JANUARY; 175 } else if (month == 2) { 176 return Calendar.FEBRUARY; 177 } else if (month == 3) { 178 return Calendar.MARCH; 179 } else if (month == 4) { 180 return Calendar.APRIL; 181 } else if (month == 5) { 182 return Calendar.MAY; 183 } else if (month == 6) { 184 return Calendar.JUNE; 185 } else if (month == 7) { 186 return Calendar.JULY; 187 } else if (month == 8) { 188 return Calendar.AUGUST; 189 } else if (month == 9) { 190 return Calendar.SEPTEMBER; 191 } else if (month == 10) { 192 return Calendar.OCTOBER; 193 } else if (month == 11) { 194 return Calendar.NOVEMBER; 195 } else if (month == 12) { 196 return Calendar.DECEMBER; 197 } else { 198 return Calendar.JANUARY; 199 } 200 } 201 } 203 | Popular Tags |