package com.basic.security.utils;
|
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Locale;
|
|
public class DateUtil {
|
|
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
|
private static SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
|
private static SimpleDateFormat yyyy年MM月dd日 = new SimpleDateFormat("yyyy年MM月dd日");
|
|
public static String format() {
|
return yyyy_MM_dd.format(new Date());
|
}
|
|
public static String format(Date date) {
|
return yyyy_MM_dd.format(date);
|
}
|
|
public static String yyyy年MM月dd日(Date date) {
|
return yyyy年MM月dd日.format(date);
|
}
|
|
public static String yyyy年MM月dd日() {
|
return yyyy年MM月dd日.format(new Date());
|
}
|
|
public static String yyyy_MM_dd_to_yyyy年MM月dd日(String date) {
|
try {
|
return yyyy年MM月dd日.format(yyyy_MM_dd.parse(date));
|
} catch (Exception e) {
|
return "";
|
}
|
}
|
|
public static String getPreviousMonthLastDay() {
|
Calendar aCalendar = Calendar.getInstance();
|
// add -1 month to current month
|
aCalendar.add(Calendar.MONTH, -1);
|
// set DATE to 1, so first date of previous month
|
aCalendar.set(Calendar.DATE, 1);
|
|
Date firstDateOfPreviousMonth = aCalendar.getTime();
|
|
// set actual maximum date of previous month
|
aCalendar.set(Calendar.DATE, aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
//read it
|
Date lastDateOfPreviousMonth = aCalendar.getTime();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
try {
|
return sdf.format(lastDateOfPreviousMonth) + " 23:59:59";
|
} catch (Exception e) {
|
e.printStackTrace();
|
return "";
|
}
|
}
|
|
public static String getYesterday() {
|
Calendar aCalendar = Calendar.getInstance();
|
aCalendar.add(Calendar.DATE, -1);
|
|
Date lastDateOfPreviousMonth = aCalendar.getTime();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
try {
|
return sdf.format(lastDateOfPreviousMonth) + " 23:59:59";
|
} catch (Exception e) {
|
e.printStackTrace();
|
return "";
|
}
|
}
|
|
public static String getNowTime() {
|
Date date = new Date(System.currentTimeMillis());
|
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
|
String s = "abc";
|
if (s instanceof String) {
|
|
}
|
return format.format(date);
|
|
}
|
|
public static String getNowTime2() {
|
Date date = new Date(System.currentTimeMillis());
|
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault());
|
return format.format(date);
|
|
}
|
|
public static String formatTime(long time, String format) {
|
Date date = new Date(time);
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
|
return simpleDateFormat.format(date);
|
|
}
|
|
public static long getDefaultTimeStamp(String time) {
|
return getTimeStamp(time, "yyyy/MM/dd HH:mm");
|
}
|
|
public static long getTimeStamp(String time, String format) {
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
Date date = new Date();
|
try {
|
date = dateFormat.parse(time);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return date.getTime();
|
}
|
|
//获得当天0点的时间戳
|
public static long getTimesMorning() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.HOUR_OF_DAY, 0);
|
cal.set(Calendar.SECOND, 0);
|
cal.set(Calendar.MINUTE, 0);
|
cal.set(Calendar.MILLISECOND, 0);
|
return cal.getTimeInMillis();
|
}
|
|
//获得当天24点的时间戳
|
public static long getTimesNight() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.HOUR_OF_DAY, 23);
|
cal.set(Calendar.SECOND, 59);
|
cal.set(Calendar.MINUTE, 59);
|
cal.set(Calendar.MILLISECOND, 999);
|
return cal.getTimeInMillis();
|
}
|
|
//获得本周一0点时间
|
public static int getTimesWeekmorning() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
return (int) (cal.getTimeInMillis() / 1000);
|
}
|
|
//获得本周日24点时间
|
public static int getTimesWeeknight() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
return (int) ((cal.getTime().getTime() + (7 * 24 * 60 * 60 * 1000)) / 1000);
|
}
|
|
//获得本月第一天0点时间
|
public static int getTimesMonthmorning() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
return (int) (cal.getTimeInMillis() / 1000);
|
}
|
// 获取当前时间是星期几
|
|
//获得本月最后一天24点时间
|
public static int getTimesMonthnight() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
cal.set(Calendar.HOUR_OF_DAY, 24);
|
return (int) (cal.getTimeInMillis() / 1000);
|
|
}
|
|
/**
|
* 判断当前日期是星期几
|
*
|
* @param pTime 要判断的时间
|
* @return dayForWeek 判断结果
|
* @Exception 发生异常
|
*/
|
public static String dayForWeek(long pTime) throws Exception {
|
Date date = new Date();
|
|
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
|
|
return dateFm.format(date);
|
}
|
|
public static String getDateTimeStrFull() {
|
return sdf.format(new Date());
|
}
|
|
}
|