1 package com.xm.utils; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.GregorianCalendar; 8 import java.util.Locale; 9 import java.util.TimeZone; 10 11 /** 12 * 时间处理工具 13 * 14 * @author xiaotangai 15 * 16 * @date 2016年8月13日上午8:52:50 17 */ 18 public class DateUtil { 19 20 private static System system; 21 private static Date date; 22 private static Calendar calendar; 23 private static TimeZone timeZone; 24 private static Locale locale; 25 private static GregorianCalendar gregorianCalendar; 26 27 private static DateFormat dateFormat; 28 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); 29 30 private static java.sql.Date sqlDate; 31 private static java.sql.Time time; 32 private static java.sql.Timestamp timestamp; 33 34 /** 35 * 获得当前时间的方法种 最基本的还是 System.currentTimeMillis() 格林威治标准时间 36 * "1970-01-01 00:00:00.000 GMT" 37 */ 38 39 /** 40 * 获得当前时间的方法1 System.currentTimeMillis() 41 * 42 * @return 43 */ 44 public static long getCurrentTimeBySystem() { 45 return System.currentTimeMillis(); 46 } 47 48 /** 49 * 获得当前时间的方法2 new Date() 50 * 51 * @return 52 */ 53 public static Date getCurrentTimeByDate() { 54 return new Date(); 55 } 56 57 /** 58 * 获得当前时间的方法3 Calendar.getInstance() 实质是 GregorianCalendar 59 * 60 * @return 61 */ 62 public static Calendar getCurrentTimeByCalendar() { 63 return Calendar.getInstance(); 64 } 65 66 /** 67 * 获得当前时间的方法4 new GregorianCalendar() 68 * 69 * @return 70 */ 71 public static GregorianCalendar getCurrentTimeByGregorianCalendar() { 72 return new GregorianCalendar(); 73 } 74 75 /** 76 * 比较两个时间 77 * 78 * @param d1 79 * @param d2 80 * @return 正数:d1>d2 0:d1=d2 负数:d1<d2 81 * 82 */ 83 public static long compare(Date d1, Date d2) { 84 return d1.getTime() - d2.getTime(); 85 } 86 87 /** 88 * 比较时间 89 * 90 * @param d1 91 * @param d2 92 * @return 93 */ 94 public static boolean after(Date d1, Date d2) { 95 d1.after(d2); 96 return compare(d1, d2) > 0; 97 } 98 99 /** 100 * 101 * @param d1 102 * @param d2 103 * @return 104 */ 105 public static boolean before(Date d1, Date d2) { 106 /* d1.before(d2); */ 107 return compare(d1, d2) < 0; 108 } 109 110 /** 111 * 通过Calendar来获得时间的各个成分 如年,月,日有等 112 * 113 * @param d 114 * @param field 115 * @return 116 */ 117 public static int getField(Date d, int field) { 118 Calendar c = Calendar.getInstance(); 119 c.setTime(d); 120 return c.get(field); 121 } 122 123 /** 124 * 通过Calendar来设置时间的各个成分 如年,月,日有等 125 * 126 * @param d 127 * @param field 128 * @param value 129 * @return 130 */ 131 public static Date setField(Date d, int field, int value) { 132 Calendar c = Calendar.getInstance(); 133 c.setTime(d); 134 c.set(field, value); 135 return c.getTime(); 136 } 137 138 /** 139 * 日期按成分增加 140 * 141 * @param d 142 * @param field 143 * @param amount 144 */ 145 public static Date add(Date d, int field, int amount) { 146 Calendar c = Calendar.getInstance(); 147 c.setTime(d); 148 c.add(field, amount); 149 return c.getTime(); 150 } 151 152 /** 153 * 日期按成分减少 154 * 155 * @param d 156 * @param field 157 * @param amount 158 * @return 159 */ 160 public static Date reduce(Date d, int field, int amount) { 161 return add(d, field, -amount); 162 } 163 164 /** 165 * 判断是否是闰年 166 * 167 * @param year 168 * @return 169 */ 170 public static boolean isLeapYear(int year) { 171 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 172 return true; 173 return false; 174 /* return new GregorianCalendar().isLeapYear(year); */ 175 } 176 177 public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; 178 public static final String DATE_PATTERN = "yyyy-MM-dd"; 179 public static final String TIME_PATTERN = "HH:mm:ss"; 180 181 /** 182 * 按格式格式化时间 183 * 184 * @param date 185 * @param pattern 186 * @return 187 */ 188 public static String format(Date date, String pattern) { 189 if (null == simpleDateFormat) { 190 simpleDateFormat = new SimpleDateFormat(); 191 } 192 simpleDateFormat.applyPattern(DATE_TIME_PATTERN); 193 return simpleDateFormat.format(date); 194 } 195 196 /** 197 * 按默认格式yyyy-MM-dd HH:mm:ss格式化时间 198 * 199 * @param date 200 * @return 201 */ 202 public static String format(Date date) { 203 return format(date, DATE_TIME_PATTERN); 204 } 205 206 /** 207 * 格式化当前时间yyyy-MM-dd HH:mm:ss 208 * 209 * @return 210 */ 211 public static String format() { 212 return format(new Date()); 213 } 214 215 /** 216 * 获得月初 217 * 218 * @param d 219 * @return 220 */ 221 public static Date getFirstDayofMonth(Date d) { 222 Calendar c = Calendar.getInstance(); 223 c.setTime(d); 224 c.set(Calendar.DAY_OF_MONTH, 1); 225 c.set(Calendar.HOUR_OF_DAY, 0); 226 c.set(Calendar.MINUTE, 0); 227 c.set(Calendar.SECOND, 0); 228 c.set(Calendar.MILLISECOND, 0); 229 return c.getTime(); 230 } 231 232 /** 233 * 获得月尾 234 * 235 * @param args 236 */ 237 public static Date getLastDayofMonth(Date d) { 238 Calendar c = Calendar.getInstance(); 239 c.setTime(d); 240 int month = c.get(Calendar.MONTH); 241 c.set(Calendar.MONDAY, month + 1); 242 c.set(Calendar.DAY_OF_MONTH, 1); 243 c.add(Calendar.DAY_OF_MONTH, -1); 244 c.set(Calendar.HOUR_OF_DAY, 23); 245 c.set(Calendar.MINUTE, 59); 246 c.set(Calendar.SECOND, 59); 247 c.set(Calendar.MILLISECOND, 999); 248 return c.getTime(); 249 } 250 251 /** 252 * 获得本周星期一 253 * 254 * @param d 255 * @return 256 */ 257 public static Date getMondayofWeek(Date d) { 258 Calendar c = Calendar.getInstance(); 259 c.setTime(d); 260 int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 261 c.add(Calendar.DAY_OF_MONTH, 2 - dayOfWeek); 262 c.set(Calendar.HOUR_OF_DAY, 0); 263 c.set(Calendar.MINUTE, 0); 264 c.set(Calendar.SECOND, 0); 265 c.set(Calendar.MILLISECOND, 0); 266 return c.getTime(); 267 } 268 269 /** 270 * 获得本周星期一 271 * 272 * @return 273 */ 274 public static Date getMonday() { 275 Calendar c = Calendar.getInstance(); 276 int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 277 c.add(Calendar.DAY_OF_MONTH, 2 - dayOfWeek); 278 c.set(Calendar.HOUR_OF_DAY, 0); 279 c.set(Calendar.MINUTE, 0); 280 c.set(Calendar.SECOND, 0); 281 c.set(Calendar.MILLISECOND, 0); 282 return c.getTime(); 283 } 284 285 /** 286 * 获得本周星期日 287 * 288 * @param args 289 */ 290 public static Date getSundayofWeek(Date d) { 291 Calendar c = Calendar.getInstance(); 292 c.setTime(d); 293 int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 294 c.add(Calendar.DAY_OF_MONTH, (8 - dayOfWeek) % 7); 295 c.set(Calendar.HOUR_OF_DAY, 23); 296 c.set(Calendar.MINUTE, 59); 297 c.set(Calendar.SECOND, 59); 298 c.set(Calendar.MILLISECOND, 999); 299 return c.getTime(); 300 } 301 302 /** 303 * 获得本周星期日 * 304 * 305 * @param args 306 */ 307 public static Date getSunday() { 308 Calendar c = Calendar.getInstance(); 309 int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 310 c.add(Calendar.DAY_OF_MONTH, (8 - dayOfWeek) % 7); 311 c.set(Calendar.HOUR_OF_DAY, 23); 312 c.set(Calendar.MINUTE, 59); 313 c.set(Calendar.SECOND, 59); 314 c.set(Calendar.MILLISECOND, 999); 315 return c.getTime(); 316 } 317 318 public static void main(String[] args) { 319 Date d = new Date(); 320 Calendar c = Calendar.getInstance(); 321 System.out.println(System.currentTimeMillis()); 322 System.out.println(d); 323 System.out.println(d.getTime()); 324 System.out.println(c); 325 System.out.println(new GregorianCalendar()); 326 System.out.println(isLeapYear(196)); 327 System.out.println(format()); 328 System.out.println(getFirstDayofMonth(d) + " " + getFirstDayofMonth(d).getTime()); 329 System.out.println(getLastDayofMonth(d) + " " + getLastDayofMonth(d).getTime()); 330 System.out.println(getMondayofWeek(d) + " " + getMondayofWeek(d).getTime()); 331 System.out.println(getSundayofWeek(d) + " " + getSundayofWeek(d).getTime()); 332 } 333 334 }
时间: 2024-12-24 07:21:49