package com.yeshi.fanli.util;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Locale;
|
import java.util.TimeZone;
|
|
public class TimeUtil {
|
|
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
public static String getMonthDay(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getHistoryTime(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getAllTime(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getWholeTime(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getAllNoTime(long time) {
|
// yyyy-MM-dd HH:mm:ss
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getyyyyMMdd(long time) {
|
// yyyy-MM-dd HH:mm:ss
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getCommonTime(long time) {
|
// yyyy-MM-dd HH:mm:ss
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
String date = sdf.format(new Date(time));
|
sdf = new SimpleDateFormat("HH:mm");
|
String hm = sdf.format(new Date(time));
|
return date + "\n" + hm;
|
}
|
|
public static String getGernalTime(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getGernalTime(long time, String format) {
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String getExchangeAdTime(long time) {
|
// yyyy-MM-dd HH:mm:ss
|
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static long convertDateToTemp(String st) {
|
Date date = new Date();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
try {
|
date = sdf.parse(st);
|
return date.getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
return 0;
|
}
|
}
|
|
public static Date parseYYYYMM(String st) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
try {
|
return sdf.parse(st);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static Date parseYYYYMMDD_HHMMSS(String st) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
try {
|
return sdf.parse(st);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static Date parseYYYYMMDD(String st) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
try {
|
return sdf.parse(st);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static Date parseDotYYYYMMDD(String st) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
|
try {
|
return sdf.parse(st);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static Date parseDotCommon(String st) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
|
try {
|
return sdf.parse(st);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static Date parseDotCommon2(String st) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
|
try {
|
return sdf.parse(st);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static long convertAllTimeToTemp(String st) {
|
Date date = new Date();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
try {
|
date = sdf.parse(st);
|
return date.getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
public static String formatDate(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
public static String formatDate(Date time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
|
String date = sdf.format(time);
|
return date;
|
}
|
|
public static String formatDateDot(Date time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
|
String date = sdf.format(time);
|
return date;
|
}
|
|
public static String formatDateAddT(Date date) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
try {
|
return sdf.format(date);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static long convertToTimeTemp(String st, String format) {
|
Date date = new Date();
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
try {
|
date = sdf.parse(st);
|
return date.getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
public static long convertDateToTemp1(String st) {
|
Date date = new Date();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
try {
|
date = sdf.parse(st);
|
return date.getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
|
public static long convertDateToTemp2(String st) {
|
Date date = new Date();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
try {
|
date = sdf.parse(st);
|
return date.getTime();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return 0;
|
}
|
|
public static boolean isSameDay(String time1, String time2) {
|
if (getGernalTime(Long.parseLong(time1)).equalsIgnoreCase(getGernalTime(Long.parseLong(time2))))
|
return true;
|
else
|
return false;
|
}
|
|
public static Date getNextDay(int day) {
|
long current = java.lang.System.currentTimeMillis();
|
long date = current / 86400000 * 86400000 - TimeZone.getDefault().getRawOffset();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(new Date(date));
|
calendar.add(Calendar.DATE, day);
|
int y = calendar.get(Calendar.YEAR);
|
int m = calendar.get(Calendar.MONTH);
|
int d = calendar.get(Calendar.DAY_OF_MONTH);
|
calendar.set(y, m, d);
|
return calendar.getTime();
|
}
|
|
public static Date getNextDay(int day, long curtime) {
|
long current = curtime;
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(new Date(current));
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.add(Calendar.DATE, day);
|
int y = calendar.get(Calendar.YEAR);
|
int m = calendar.get(Calendar.MONTH);
|
int d = calendar.get(Calendar.DAY_OF_MONTH);
|
calendar.set(y, m, d);
|
return calendar.getTime();
|
}
|
|
public static List<String> getEmupDate(int days) {
|
days = -days + 1;
|
List<String> list = new ArrayList<String>();
|
for (int ii = days; ii <= 0; ii++) {
|
list.add(dateFormat.format(getNextDay(ii)));
|
}
|
return list;
|
}
|
|
public static List<String> getEmupDate(int days, long endtime) {
|
days = -days;
|
List<String> list = new ArrayList<String>();
|
for (int ii = days; ii <= 0; ii++) {
|
list.add(dateFormat.format(getNextDay(ii, endtime)));
|
}
|
return list;
|
}
|
|
public static String getSimpleDate(Date date) {
|
String format = dateFormat.format(date);
|
return format;
|
}
|
|
public static String getSimpleDateMonth(Date date) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
String format = sdf.format(date);
|
return format;
|
}
|
|
public static String getMonthOnlyMM(Date date) {
|
SimpleDateFormat sdf = new SimpleDateFormat("MM");
|
String format = sdf.format(date);
|
return format;
|
}
|
|
public static String getYearOnlyYYYY(Date date) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
|
String format = sdf.format(date);
|
return format;
|
}
|
|
public static int getDayOfMonth() {
|
Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
|
int day = aCalendar.getActualMaximum(Calendar.DATE);
|
return day;
|
}
|
|
public static Date parse(String time) throws ParseException {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
return sdf.parse(time);
|
}
|
|
public static String yyyyMMddHHmmss(long time) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
String date = sdf.format(new Date(time));
|
return date;
|
}
|
|
/**
|
* 获取两个时间的天数差值
|
*
|
* @param from
|
* @param to
|
* @return
|
*/
|
public static int getDayDifferenceCount(Date from, Date to) {
|
Calendar cal1 = Calendar.getInstance();
|
cal1.setTime(from);
|
Calendar cal2 = Calendar.getInstance();
|
cal2.setTime(to);
|
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
|
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
|
int year1 = cal1.get(Calendar.YEAR);
|
int year2 = cal2.get(Calendar.YEAR);
|
if (year1 != year2) { // 同一年
|
int timeDistance = 0;
|
for (int i = year1; i < year2; i++) {
|
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { // 闰年
|
timeDistance += 366;
|
} else { // 不是闰年
|
timeDistance += 365;
|
}
|
}
|
return timeDistance + (day2 - day1);
|
} else { // 不同年
|
return day2 - day1;
|
}
|
|
}
|
}
|