Java日期工具类,Java时间工具类,Java时间格式化
Posted 蕃薯耀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java日期工具类,Java时间工具类,Java时间格式化相关的知识,希望对你有一定的参考价值。
Java日期工具类,Java时间工具类,Java时间格式化
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年2月4日 15:03:27 星期六
http://www.cnblogs.com/fanshuyao/
附件下载见:http://fanshuyao.iteye.com/blog/2355386
1 package com.chinagas.common.utils; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 8 9 public class DateUtils { 10 11 public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; 12 public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm"; 13 public static final String HOUR_PATTERN = "yyyy-MM-dd HH:mm:ss"; 14 public static final String DATE_PATTERN = "yyyy-MM-dd"; 15 public static final String MONTH_PATTERN = "yyyy-MM"; 16 public static final String YEAR_PATTERN = "yyyy"; 17 public static final String MINUTE_ONLY_PATTERN = "mm"; 18 public static final String HOUR_ONLY_PATTERN = "HH"; 19 20 /** 21 * 日期相加减天数 22 * @param date 如果为Null,则为当前时间 23 * @param days 加减天数 24 * @param includeTime 是否包括时分秒,true表示包含 25 * @return 26 * @throws ParseException 27 */ 28 public static Date dateAdd(Date date, int days, boolean includeTime) throws ParseException{ 29 if(date == null){ 30 date = new Date(); 31 } 32 if(!includeTime){ 33 SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_PATTERN); 34 date = sdf.parse(sdf.format(date)); 35 } 36 Calendar cal = Calendar.getInstance(); 37 cal.setTime(date); 38 cal.add(Calendar.DATE, days); 39 return cal.getTime(); 40 } 41 42 /** 43 * 时间格式化成字符串 44 * @param date Date 45 * @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN, 如果为空,则为yyyy-MM-dd 46 * @return 47 * @throws ParseException 48 */ 49 public static String dateFormat(Date date, String pattern) throws ParseException{ 50 if(StrUtils.isBlank(pattern)){ 51 pattern = DateUtils.DATE_PATTERN; 52 } 53 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 54 return sdf.format(date); 55 } 56 57 /** 58 * 字符串解析成时间对象 59 * @param dateTimeString String 60 * @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN,如果为空,则为yyyy-MM-dd 61 * @return 62 * @throws ParseException 63 */ 64 public static Date dateParse(String dateTimeString, String pattern) throws ParseException{ 65 if(StrUtils.isBlank(pattern)){ 66 pattern = DateUtils.DATE_PATTERN; 67 } 68 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 69 return sdf.parse(dateTimeString); 70 } 71 72 /** 73 * 将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化) 74 * @param dateTime Date 75 * @return 76 * @throws ParseException 77 */ 78 public static String dateTimeToDateString(Date dateTime) throws ParseException{ 79 String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN); 80 return dateTimeString.substring(0, 10); 81 } 82 83 /** 84 * 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串, 85 * 当时、分、秒不为00:00:00时,直接返回 86 * @param dateTime Date 87 * @return 88 * @throws ParseException 89 */ 90 public static String dateTimeToDateStringIfTimeEndZero(Date dateTime) throws ParseException{ 91 String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN); 92 if(dateTimeString.endsWith("00:00:00")){ 93 return dateTimeString.substring(0, 10); 94 }else{ 95 return dateTimeString; 96 } 97 } 98 99 /** 100 * 将日期时间格式成日期对象,和dateParse互用 101 * @param dateTime Date 102 * @return Date 103 * @throws ParseException 104 */ 105 public static Date dateTimeToDate(Date dateTime) throws ParseException{ 106 Calendar cal = Calendar.getInstance(); 107 cal.setTime(dateTime); 108 cal.set(Calendar.HOUR_OF_DAY, 0); 109 cal.set(Calendar.MINUTE, 0); 110 cal.set(Calendar.SECOND, 0); 111 cal.set(Calendar.MILLISECOND, 0); 112 return cal.getTime(); 113 } 114 115 /** 116 * 时间加减小时 117 * @param startDate 要处理的时间,Null则为当前时间 118 * @param hours 加减的小时 119 * @return Date 120 */ 121 public static Date dateAddHours(Date startDate, int hours) { 122 if (startDate == null) { 123 startDate = new Date(); 124 } 125 Calendar c = Calendar.getInstance(); 126 c.setTime(startDate); 127 c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours); 128 return c.getTime(); 129 } 130 131 /** 132 * 时间加减分钟 133 * @param startDate 要处理的时间,Null则为当前时间 134 * @param minutes 加减的分钟 135 * @return 136 */ 137 public static Date dateAddMinutes(Date startDate, int minutes) { 138 if (startDate == null) { 139 startDate = new Date(); 140 } 141 Calendar c = Calendar.getInstance(); 142 c.setTime(startDate); 143 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes); 144 return c.getTime(); 145 } 146 147 /** 148 * 时间加减秒数 149 * @param startDate 要处理的时间,Null则为当前时间 150 * @param minutes 加减的秒数 151 * @return 152 */ 153 public static Date dateAddSeconds(Date startDate, int seconds) { 154 if (startDate == null) { 155 startDate = new Date(); 156 } 157 Calendar c = Calendar.getInstance(); 158 c.setTime(startDate); 159 c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds); 160 return c.getTime(); 161 } 162 163 /** 164 * 时间加减天数 165 * @param startDate 要处理的时间,Null则为当前时间 166 * @param days 加减的天数 167 * @return Date 168 */ 169 public static Date dateAddDays(Date startDate, int days) { 170 if (startDate == null) { 171 startDate = new Date(); 172 } 173 Calendar c = Calendar.getInstance(); 174 c.setTime(startDate); 175 c.set(Calendar.DATE, c.get(Calendar.DATE) + days); 176 return c.getTime(); 177 } 178 179 /** 180 * 时间加减月数 181 * @param startDate 要处理的时间,Null则为当前时间 182 * @param months 加减的月数 183 * @return Date 184 */ 185 public static Date dateAddMonths(Date startDate, int months) { 186 if (startDate == null) { 187 startDate = new Date(); 188 } 189 Calendar c = Calendar.getInstance(); 190 c.setTime(startDate); 191 c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months); 192 return c.getTime(); 193 } 194 195 /** 196 * 时间加减年数 197 * @param startDate 要处理的时间,Null则为当前时间 198 * @param years 加减的年数 199 * @return Date 200 */ 201 public static Date dateAddYears(Date startDate, int years) { 202 if (startDate == null) { 203 startDate = new Date(); 204 } 205 Calendar c = Calendar.getInstance(); 206 c.setTime(startDate); 207 c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years); 208 return c.getTime(); 209 } 210 211 /** 212 * 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0) 213 * @param myDate 时间 214 * @param compareDate 要比较的时间 215 * @return int 216 */ 217 public static int dateCompare(Date myDate, Date compareDate) { 218 Calendar myCal = Calendar.getInstance(); 219 Calendar compareCal = Calendar.getInstance(); 220 myCal.setTime(myDate); 221 compareCal.setTime(compareDate); 222 return myCal.compareTo(compareCal); 223 } 224 225 /** 226 * 获取两个时间中最小的一个时间 227 * @param date 228 * @param compareDate 229 * @return 230 */ 231 public static Date dateMin(Date date, Date compareDate) { 232 if(date == null){ 233 return compareDate; 234 } 235 if(compareDate == null){ 236 return date; 237 } 238 if(1 == dateCompare(date, compareDate)){ 239 return compareDate; 240 }else if(-1 == dateCompare(date, compareDate)){ 241 return date; 242 } 243 return date; 244 } 245 246 /** 247 * 获取两个时间中最大的一个时间 248 * @param date 249 * @param compareDate 250 * @return 251 */ 252 public static Date dateMax(Date date, Date compareDate) { 253 if(date == null){ 254 return compareDate; 255 } 256 if(compareDate == null){ 257 return date; 258 } 259 if(1 == dateCompare(date, compareDate)){ 260 return date; 261 }else if(-1 == dateCompare(date, compareDate)){ 262 return compareDate; 263 } 264 return date; 265 } 266 267 /** 268 * 获取两个日期(不含时分秒)相差的天数,不包含今天 269 * @param startDate 270 * @param endDate 271 * @return 272 * @throws ParseException 273 */ 274 public static int dateBetween(Date startDate, Date endDate) throws ParseException { 275 Date dateStart = dateParse(dateFormat(startDate, DATE_PATTERN), DATE_PATTERN); 276 Date dateEnd = dateParse(dateFormat(endDate, DATE_PATTERN), DATE_PATTERN); 277 return (int) ((dateEnd.getTime() - dateStart.getTime())/1000/60/60/24); 278 } 279 280 /** 281 * 获取两个日期(不含时分秒)相差的天数,包含今天 282 * @param startDate 283 * @param endDate 284 * @return 285 * @throws ParseException 286 */ 287 public static int dateBetweenIncludeToday(Date startDate, Date endDate) throws ParseException { 288 return dateBetween(startDate, endDate) + 1; 289 } 290 291 /** 292 * 获取日期时间的年份,如2017-02-13,返回2017 293 * @param date 294 * @return 295 */ 296 public static int getYear(Date date) { 297 Calendar cal = Calendar.getInstance(); 298 cal.setTime(date); 299 return cal.get(Calendar.YEAR); 300 } 301 302 /** 303 * 获取日期时间的月份,如2017年2月13日,返回2 304 * @param date 305 * @return 306 */ 307 public static int getMonth(Date date) { 308 Calendar cal = Calendar.getInstance(); 309 cal.setTime(date); 310 return cal.get(Calendar.MONTH) + 1; 311 } 312 313 /** 314 * 获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13 315 * @param date 316 * @return 317 */ 318 public static int getDate(Date date) { 319 Calendar cal = Calendar.getInstance(); 320 cal.setTime(date); 321 return cal.get(Calendar.DATE); 322 } 323 324 /** 325 * 获取日期时间当月的总天数,如2017-02-13,返回28 326 * @param date 327 * @return 328 */ 329 public static int getDaysOfMonth(Date date) { 330 Calendar cal = Calendar.getInstance(); 331 cal.setTime(date); 332 return cal.getActualMaximum(Calendar.DATE); 333 } 334 335 /** 336 * 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数 337 * @param date 338 * @return 339 */ 340 public static int getDaysOfYear(Date date) { 341 Calendar cal = Calendar.getInstance(); 342 cal.setTime(date); 343 return cal.getActualMaximum(Calendar.DAY_OF_YEAR); 344 } 345 346 /** 347 * 根据时间获取当月最大的日期 348 * <li>2017-02-13,返回2017-02-28</li> 349 * <li>2016-02-13,返回2016-02-29</li> 350 * <li>2016-01-11,返回2016-01-31</li> 351 * @param date Date 352 * @return 353 * @throws Exception 354 */ 355 public static Date maxDateOfMonth(Date date) throws Exception { 356 Calendar cal = Calendar.getInstance(); 357 cal.setTime(date); 358 int value = cal.getActualMaximum(Calendar.DATE); 359 return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null); 360 } 361 362 /** 363 * 根据时间获取当月最小的日期,也就是返回当月的1号日期对象 364 * @param date Date 365 * @return 366 * @throws Exception 367 */ 368 public static Date minDateOfMonth(Date date) throws Exception { 369 Calendar cal = Calendar.getInstance(); 370 cal.setTime(date); 371 int value = cal.getActualMinimum(Calendar.DATE); 372 return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null); 373 } 374 375 public static void main(String[] args) throws Exception { 376 /*System.out.println(dateTimeToDate(new Date())); 377 System.out.println(dateParse("2017-02-04 14:58:20", null)); 378 System.out.println(dateTimeToDateStringIfTimeEndZero(new Date())); 379 System.out.println(dateTimeToDateStringIfTimeEndZero(dateTimeToDate(new Date())));*/ 380 //System.out.println(dateBetween(dateParse("2017-01-30", null), dateParse("2017-02-01", null))); 381 //System.out.println(dateBetweenIncludeToday(dateParse("2017-01-30", null), dateParse("2017-02-01", null))); 382 System.out.println(getDate(dateParse("2017-01-17", null))); 383 /* 384 System.out.printl以上是关于Java日期工具类,Java时间工具类,Java时间格式化的主要内容,如果未能解决你的问题,请参考以下文章