Java時間処理ツールクラスDateUtils
8812 ワード
以下はいくつかのよくあるJavaバックグラウンドの時間処理のツールのパッケージを貼って、一部はオープンソースの作者ThinkGemのを参考にして、一部は自分で追加しました;
/**
* Copyright © 2012-2014 JeeSite All rights reserved.
*/
package com.thinkgem.jeesite.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
* , org.apache.commons.lang.time.DateUtils
*
* @author ThinkGem
* @version 2014-4-15
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };
/**
* (yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* (yyyy-MM-dd) pattern :"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* (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 formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* , (yyyyMMddHHmmss)
*/
public static String getDateTime14() {
long currTime = System.currentTimeMillis();
Date date = new Date(currTime);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(date);
}
/**
* (HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* (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)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* (dd)
*/
public static String getDay() {
return formatDate(new Date(), "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", "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 parseDate(str.toString(), parsePatterns);
}
catch (ParseException e) {
return null;
}
}
/**
* EEE MMM dd HH:mm:ss zzz yyyy
*
* @param time
* @return
*/
public static Date parseDateString(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date d = new Date();
try {
d = sdf.parse(time);
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
/**
*
*
* @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 pastHour(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 60 * 1000);
}
/**
*
*
* @param date
* @return
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 1000);
}
/**
* ( , : : . )
*
* @param timeMillis
* @return
*/
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
/**
*
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
/**
*
*
* @param date
*
* @param
* @return
*/
public static Date calculateDate(Date date, int num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.DATE, num);
return calendar.getTime();
}
/**
*
*
* @param newDate
* @param oldDate
* @return
*/
public static Integer differDate(Date newDate, Date oldDate) {
int days = (int) ((newDate.getTime() - oldDate.getTime()) / (1000 * 3600 * 24));
return Math.abs(days);
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// System.out.println(formatDate(parseDate("2010/3/6")));
// System.out.println(getDate("yyyy MM dd E"));
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
// System.out.println(time/(24*60*60*1000));
String time = "Tue Nov 24 00:00:00 CST 2015";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date d = sdf.parse(time);
System.out.println(d);
}
/**
* yyyy-MM-dd
*
* @author cao_wencao
* @return
*/
public static String getMonthFristDay() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// :
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);// 1 ,
String first = format.format(c.getTime());
return first;
}
/**
* yyyy-MM-dd
*
* @author cao_wencao
* @return
*/
public static String getMonthLastDay() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("===============last:" + last);
return last;
}
/**
* Date
* @author cao_wencao
* @param unixTime
* @return
*/
public static Date unixTimeToDate(Long unixTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long time = unixTime * 1000;
String d = format.format(time);
Date date = null;
try {
return format.parse(d);
}
catch (ParseException e) {
e.getMessage();
e.printStackTrace();
}
return date;
}
}