Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.oa.product.action; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; /** * * * @author chenpeng * @version 2014-09-29 */ public class MyDateUtils { private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }; /** * ? ?yyyy-MM-dd */ public static String getDate() { return DateFormatUtils.format(new Date(), "yyyy-MM-dd"); } /** * ??yyyy-MM-dd HH:mm:ss */ public static String getDateTime(Date date) { return formatDate(date, "yyyy-MM-dd HH:mm:ss"); } /** * ?yyyy-MM-dd pattern?"yyyy-MM-dd HH:mm:ss E" */ public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } /** * ? ?yyyy-MM-dd HH:mm:ss */ public static String getDateTime() { return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } /** * ? ?yyyy */ public static String getYear() { return formatDate(new Date(), "yyyy"); } /** * ? ?MM.dd */ public static String getMonthdd() { return formatDate(new Date(), "MM.dd"); } /** * ? ?E| */ public static String getWeek() { return formatDate(new Date(), "E"); } /** * ? { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" } */ public static Date parseDate(Object str) { if (str == null) { return null; } try { return DateUtils.parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } } /** * ? * * @param date * @return */ public static long pastDays(Date date) { long t = new Date().getTime() - date.getTime(); return t / (24 * 60 * 60 * 1000); } /** * ? * * @param date * @return */ public static long distanceDays(Date date1, Date date2) { long t = date2.getTime() - date1.getTime(); return t / (24 * 60 * 60 * 1000); } /** * ???"yyyy-MM-dd HH:mm:ss" * * @param date * @return */ public static Date getDateStart(Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 00:00:00"); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * ????"yyyy-MM-dd HH:mm:ss" * * @param date * @return */ public static Date getDateEnd(Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 23:59:59"); } catch (ParseException e) { e.printStackTrace(); } return date; } }