下面的方法可以得到三天前的具體時間-->
Date start=new Date();//取時間
Calendar calendar = new GregorianCalendar();
calendar.setTime(start);
calendar.add(calendar.DATE,-3);//把日期往前推3天
start=calendar.getTime();
我們的用Gson轉化的Date類型數據到前臺會轉化爲一個時間戳:
new Gson().toJson(對象索引)
也可以使用普遍的轉換方法-->
Date date = new Date();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayTime = formate.format(date);
下面介紹一個神奇的時間工具類:
package com.jd.ecc.commons.lib.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateFormatUtil {
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String YEAR_MONTH_FORMAT = "yyyy-MM";
public DateFormatUtil() {
}
public static boolean isValidDate(String dateStr) {
return validFormat(dateStr, "yyyy-MM-dd");
}
public static boolean isValidDate(String datetStr, String dateFormat) {
return validFormat(datetStr, dateFormat);
}
public static boolean isValidDateTime(String datetimeStr) {
return validFormat(datetimeStr, "yyyy-MM-dd HH:mm:ss");
}
public static boolean isValidDateTime(String datetimeStr, String datetimeFormat) {
return validFormat(datetimeStr, datetimeFormat);
}
public static boolean validFormat(String str, String format) {
boolean convertSuccess = true;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
try {
simpleDateFormat.setLenient(false);
Date e = simpleDateFormat.parse(str);
Date maxDate = simpleDateFormat.parse("9999-12-31");
if(e.after(maxDate)) {
convertSuccess = false;
}
} catch (ParseException var6) {
convertSuccess = false;
}
return convertSuccess;
}
public static String getTodayTime() {
Date date = new Date();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayTime = formate.format(date);
return todayTime;
}
public static String getTodatDate() {
Date date = new Date();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
String todayDate = formate.format(date);
return todayDate;
}
public static String getYesterdayDate() {
Date date = new Date();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(5, -1);
date = calendar.getTime();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
String yesterdayDate = formate.format(date);
return yesterdayDate;
}
public static String getYesterdayTime() {
Date date = new Date();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(5, -1);
date = calendar.getTime();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String yesterdayTime = formate.format(date);
return yesterdayTime;
}
public static String getNowMonth() {
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
String nowMonth = df.format(d);
System.out.println(nowMonth);
return nowMonth;
}
public static String getLastMonth() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
int validMonth = cd.get(2) - 1;
cd.set(2, validMonth);
Date dt = cd.getTime();
String lastMonth = df.format(dt);
return lastMonth;
}
public static String getTodayBeginTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.set(11, 0);
cd.set(12, 0);
cd.set(13, 0);
Date dt = cd.getTime();
String todayBeginTime = df.format(dt);
return todayBeginTime;
}
public static String getTodayEndTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.set(11, 23);
cd.set(12, 59);
cd.set(13, 59);
Date dt = cd.getTime();
String todayEndTime = df.format(dt);
return todayEndTime;
}
public static String getYesterdayBeginTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int yesterday = cd.get(5) - 1;
cd.set(5, yesterday);
cd.set(11, 0);
cd.set(12, 0);
cd.set(13, 0);
Date dt = cd.getTime();
String yesterdayBeginTime = df.format(dt);
return yesterdayBeginTime;
}
public static String getYesterdayEndTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int yesterday = cd.get(5) - 1;
cd.set(5, yesterday);
cd.set(11, 23);
cd.set(12, 59);
cd.set(13, 59);
Date dt = cd.getTime();
String yesterdayEndTime = df.format(dt);
return yesterdayEndTime;
}
public static String getNowMonthBeginTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.add(2, 0);
cd.set(5, 1);
cd.set(11, 0);
cd.set(12, 0);
cd.set(13, 0);
Date dt = cd.getTime();
String nowMonthBeginTime = df.format(dt);
return nowMonthBeginTime;
}
public static String getNowMonthBeginDate() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cd.add(2, 0);
cd.set(5, 1);
Date dt = cd.getTime();
String nowMonthBeginDate = df.format(dt);
return nowMonthBeginDate;
}
public static String getNowMonthEndTime() {
Calendar ca = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ca.set(5, ca.getActualMaximum(5));
ca.set(11, 23);
ca.set(12, 59);
ca.set(13, 59);
String nowMonthEndTime = df.format(ca.getTime());
return nowMonthEndTime;
}
public static String getNowMonthEndDate() {
Calendar ca = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ca.set(5, ca.getActualMaximum(5));
String nowMonthEndDate = df.format(ca.getTime());
return nowMonthEndDate;
}
public static String getLastMonthBeginTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.add(2, -1);
cd.set(5, 1);
cd.set(11, 0);
cd.set(12, 0);
cd.set(13, 0);
Date dt = cd.getTime();
String lastMonthBeginTime = df.format(dt);
return lastMonthBeginTime;
}
public static String getLastMonthBeginDate() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cd.add(2, -1);
cd.set(5, 1);
Date dt = cd.getTime();
String lastMonthBeginDate = df.format(dt);
return lastMonthBeginDate;
}
public static String getLastMonthEndTime() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.set(5, 0);
cd.set(11, 23);
cd.set(12, 59);
cd.set(13, 59);
Date dt = cd.getTime();
String lastMonthEndTime = df.format(dt);
return lastMonthEndTime;
}
public static String getLastMonthEndDate() {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cd.set(5, 0);
Date dt = cd.getTime();
String lastMonthEndDate = df.format(dt);
return lastMonthEndDate;
}
public static String getAfterDaysTime(Integer dateLen) {
GregorianCalendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.add(5, dateLen.intValue());
cd.set(11, 23);
cd.set(12, 59);
cd.set(13, 59);
Date dt = cd.getTime();
String afterThirtyDaysDate = df.format(dt);
return afterThirtyDaysDate;
}
public static String getNowWeekBeginTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cds = Calendar.getInstance();
cds.setFirstDayOfWeek(2);
cds.set(11, 0);
cds.set(12, 0);
cds.set(13, 0);
cds.set(7, 2);
String thisweekstart = df.format(cds.getTime());
return thisweekstart;
}
public static String getNowWeekEndTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cde = Calendar.getInstance();
cde.setFirstDayOfWeek(2);
cde.set(11, 23);
cde.set(12, 59);
cde.set(13, 59);
cde.set(7, 1);
String thisweekend = df.format(cde.getTime());
return thisweekend;
}
public static String getLastWeekBeginTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cdl = Calendar.getInstance();
cdl.setFirstDayOfWeek(2);
cdl.add(4, -1);
cdl.set(7, 1);
cdl.set(11, 0);
cdl.set(12, 0);
cdl.set(13, 0);
cdl.set(7, 2);
String lastweekstart = df.format(cdl.getTime());
return lastweekstart;
}
public static String getLastWeekEndTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cdle = Calendar.getInstance();
cdle.setFirstDayOfWeek(2);
cdle.add(4, -1);
cdle.set(7, 1);
cdle.set(11, 23);
cdle.set(12, 59);
cdle.set(13, 59);
String lastweekend = df.format(cdle.getTime());
return lastweekend;
}
public static String getThreeMonthAgoTime() {
new Date();
Calendar cald = Calendar.getInstance();
cald.add(2, -3);
Date date = cald.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String threeMonthAgoTime = df.format(date);
return threeMonthAgoTime;
}
public static String getThreeMonthAgoDate() {
new Date();
Calendar cald = Calendar.getInstance();
cald.add(2, -3);
Date date = cald.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String threeMonthAgoDate = df.format(date);
return threeMonthAgoDate;
}
}
功能簡介-->方法返回類型 | 方法名稱 | 方法含義 |
---|---|---|
方法返回類型 | 方法名稱 | 方法含義 |
boolean | isValidDate(String dateStr) | 根據默認格式校驗日期 |
boolean | isValidDate(String datetStr, String dateFormat) | 自定義格式校驗日期 |
boolean | isValidDateTime(String datetimeStr) | 根據默認格式校驗時間 |
boolean | isValidDateTime(String datetimeStr, String datetimeFormat) | 自定義格式校驗時間 |
boolean | validFormat(String str, String format) | 格式校驗抽象方法 |
String | getTodayTime() | 獲得今天具體時間 |
String | getTodatDate() | 獲得今天日期 |
String | getYesterdayDate() | 獲得昨天日期 |
String | getYesterdayTime() | 獲得昨天具體時間 |
String | getNowMonth() | 獲取當前月 |
String | getLastMonth() | 獲取上一個月 |
String | getTodayBeginTime() | 獲取當天的開始時間 |
String | getTodayEndTime | 獲取當天的結束時間 |
String | getYesterdayBeginTime() | 獲取前一天的開始時間 |
String | getYesterdayEndTime() | 獲取前一天的結束時間 |
String | getNowMonthBeginTime() | 獲得當前月的第一天開始時間 |
String | getNowMonthBeginDate() | 獲得當前月的第一天開始日期 |
String | getNowMonthEndTime() | 獲得當前月最後一天結束時間 |
String | getNowMonthEndDate() | 獲得當前月最後一天結束日期 |
String | getLastMonthBeginTime() | 獲得前一月的第一天開始時間 |
String | getLastMonthBeginDate() | 獲得前一月的第一天開始日期 |
String | getLastMonthEndTime() | 獲得前一月的最後一天結束時間 |
String | getLastMonthEndDate() | 獲得前一月的最後一天結束日期 |
String | getAfterDaysTime(Integer dateLen) | 獲得當前時間後X天之後的結束時間 |
String | getNowWeekBeginTime() | 獲得本週開始時間 |
String | getNowWeekEndTime() | 獲得本週結束時間 |
String | getLastWeekBeginTime() | 獲得上週開始時間 |
String | getLastWeekEndTime() | 獲得上週結束時間 |
String | getThreeMonthAgoTime() | 獲得3個月前時間 |
String | getThreeMonthAgoDate() | 獲得3個月前日期 |