package com.jiuqi.mobile.nigo.comeclose.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
*
* @author modi
*
*/
public final class DateUtils {
/**
* 一天的毫秒数
*/
public static final long ONE_HOUR_TIME = 60 * 60 * 1000l;
public static final long ONE_DATE_TIME = 24 * ONE_HOUR_TIME;
// private static final long OneDateSub = 8 * 60 * 60 * 1000l;
/**
* 一周的毫秒数
*/
public static final long ONE_WEEK_TIME = 7 * ONE_DATE_TIME;
public static final long OneMonthEmpty30 = 30 * ONE_DATE_TIME;
public static final long OneMonthEmpty31 = 31 * ONE_DATE_TIME;
public static final long OneMonthEmpty29 = 29 * ONE_DATE_TIME;
public static final long OneMonthEmpty28 = 28 * ONE_DATE_TIME;
private static final String defaultSDFStr = "yyyy-MM-dd HH:mm:ss.SSS";
private static final String dateSDFStr2 = "yyyy-MM-dd HH:mm:ss";
private static final String dateSDFStr = "yyyy-MM-dd";
private static final SimpleDateFormat DefaultSDF = new SimpleDateFormat(
defaultSDFStr);
private static final SimpleDateFormat dateSDF = new SimpleDateFormat(
dateSDFStr);
private static final SimpleDateFormat dateSDF2 = new SimpleDateFormat(
dateSDFStr2);
// private static final Calendar defaultCal =
// Calendar.getInstance(Locale.CHINESE);
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
public static void main(String[] args) {
// System.out.println("day start:" + getDateString(getDayStartTime()));
// System.out.println("day end:" + getDateString(getDayEndTime()));
// System.out.println("week start:" +
// getDateString(getWeekStartTime()));
// System.out.println("Month start:" +
// getDateString(getMonthStartTime()));
// System.out.println("month:" + getMonth());
// System.out.println("week:" + getWeek(System.currentTimeMillis()));
// System.out.println("year:" + getYear(System.currentTimeMillis()));
// System.out.println("lunar:"
// + getDateString(getLunarCalendar(System.currentTimeMillis())));
// long ll = 1399305600000l;
// System.out.println(sdf.format(new Date(ll)));
// long l = getWeekStartTime(1399305600000l);
// long l = getWeekStartTime(ll);
System.out.println(sdf.format(new Date(1434384000979l)));
// l = getWeekStartTime();
System.out.println(sdf.format(new Date(1434556800426l)));
System.out.println("===="+StringToDate(getDateString2(System
.currentTimeMillis())));
}
/**
* 本月开始时间
*
* @return
*/
public static long getMonthStartTime() {
return getMonthStartTime(System.currentTimeMillis());
}
/**
* 获得前3个月第一天的开始时间 例: 当前是10月,那么返回的是8月1号0点的时间
*
* @return
*/
public static long getThreeMonthStartTime() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -2);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTimeInMillis());
}
/**
* 本周开始时间
*
* @return
*/
public static long getWeekStartTime() {
return getWeekStartTime(System.currentTimeMillis());
}
/**
* 月开始时间
*
* @return
*/
public static long getMonthStartTime(long l) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(l);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime().getTime());
}
/**
* 周开始时间
*
* @return
*/
public static long getWeekStartTime(long l) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(l);
// int year = cal.get(Calendar.YEAR);
// int month = cal.get(Calendar.MONTH);
// cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
// cal.get(Calendar.DAY_OF_MONTH);
// System.out.println(sdf.format(cal.getTime()));
if (7 == getWeek(l))
return getDayStartTime(cal.getTime().getTime()) - ONE_WEEK_TIME;
else
return getDayStartTime(cal.getTime().getTime());
}
/**
* 当前月份
*
* @return
*/
public static int getMonth() {
return getMonth(System.currentTimeMillis());
}
/**
* 当前年份
*
* @param l
* @return
*/
public static int getYear() {
return getYear(System.currentTimeMillis());
}
/**
* 当前星期数
*
* @return
*/
public static int getWeek() {
return getWeek(System.currentTimeMillis());
}
/**
* 月份
*
* @param l
* @return
*/
public static int getMonth(long l) {
Calendar cal = Calendar.getInstance(Locale.CHINESE);
cal.setTimeInMillis(l);
return cal.get(Calendar.MONTH) + 1;
}
/**
* 年份
*
* @param l
* @return
*/
public static int getYear(long l) {
Calendar cal = Calendar.getInstance(Locale.CHINESE);
cal.setTimeInMillis(l);
return cal.get(Calendar.YEAR);
}
/**
* 日期
*
* @param l
* @return
*/
public static int getDay(long l) {
Calendar cal = Calendar.getInstance(Locale.CHINESE);
cal.setTimeInMillis(l);
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* 天(年)
*
* @param l
* @return
*/
public static int getDayByYear(long l) {
Calendar cal = Calendar.getInstance(Locale.CHINESE);
cal.setTimeInMillis(l);
return cal.get(Calendar.DAY_OF_YEAR);
}
/**
* 是否月末(比较到日期)
*
* @param l
* @return
*/
public static boolean isMouthEnd(long l) {
int month = getMonth(l);
int day = getDay(l);
if (isIntIn(month, 1, 3, 5, 7, 8, 10, 12)) {
if (31 == day) {
return true;
}
} else if (isIntIn(month, 4, 6, 9, 11)) {
if (30 == day) {
return true;
}
} else if (isLeapYear(l)) {
return 29 == day;
} else {
return 28 == day;
}
return false;
}
private static boolean isIntIn(int thisInt, int... is) {
if (null == is) {
return false;
}
for (int i : is) {
if (thisInt == i) {
return true;
}
}
return false;
}
/**
* 是否是闰年
*
* @return
*/
public static boolean isLeapYear(long l) {
int year = getYear(l);
return 0 == year % 4 || 0 == year % 100 || 0 == year % 400;
}
/**
* 是否年末
*
* @param l
* @return
*/
public static boolean isYearEnd(long l) {
Calendar cal = Calendar.getInstance(Locale.CHINESE);
cal.setTimeInMillis(l);
int day = cal.get(Calendar.DAY_OF_YEAR);
if (isLeapYear(l)) {
return 366 == day;
} else {
return 365 == day;
}
}
/**
* 星期数
*
* @param l
* @return
*/
public static int getWeek(long l) {
Calendar cal = Calendar.getInstance(Locale.CHINESE);
cal.setTimeInMillis(l);
int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
return week == 0 ? 7 : week;
}
/**
* 当天开始时间0:00:00:000
*
* @return
*/
public static long getDayStartTime() {
return getDayStartTime(System.currentTimeMillis());
}
/**
* 当天结束时间23:59:59:999
*
* @return
*/
public static long getDayEndTime() {
return getDayStartTime() + ONE_DATE_TIME - 1l;
}
/**
* 当天开始时间0:00:00:000
*
* @return
*/
public static long getDayStartTime(long l) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(l);
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
return c.getTimeInMillis();
// c.getFirstDayOfWeek();
// c.set(field, value);
// return (l / ONE_DATE_TIME) * ONE_DATE_TIME - OneDateSub;
}
/**
* 当天结束时间23:59:59:999
*
* @return
*/
public static long getDayEndTime(long l) {
return getDayStartTime(l) + ONE_DATE_TIME - 1l;
}
/**
* 获得时间字符串。默认"yyyy-MM-dd HH:mm:ss.SSS"
*
* @param l
* @return
*/
public static String getDateString(long l) {
return DefaultSDF.format(new Date(l));
}
/**
* 获得时间字符串。默认"yyyy-MM-dd"
*
* @param l
* @return
*/
public static String getDateString2(long l) {
return dateSDF.format(new Date(l));
}
/**
* 获得时间字符串。默认"yyyy-MM-dd"
*
* @param l
* @return
*/
public static String getDateString3(long l) {
return dateSDF2.format(new Date(l));
}
/**
* 获得时间字符串。默认"yyyy-MM-dd HH:mm:ss.SSS"
* @param l
* @param dateFormat
* @return
*/
public static String getDateString(long l, String dateFormat) {
return new SimpleDateFormat(dateFormat == null ? defaultSDFStr
: dateFormat).format(new Date(l));
}
/**
* @Title: getDate
* @Description: String型转换为特定格式的日期类型
* @param @param str
* @param @param dateFormat
* @param @return 设定文件
* @return long 返回类型
* @throws
*/
public static long getDate(String str, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
try {
return sdf.parse(str).getTime();
} catch (ParseException e) {
return 0;
}
}
/**
* 天数差
* @param startTime 初始时间
* @param endTime 结束时间
* @param haveHour 是否有让时间参与计算。默认false。只记天数差。
* @return
*/
public static int subDay(long startTime, long endTime) {
return subDay(startTime, endTime, false);
}
/**
* 天数差
* @param startTime 初始时间
* @param endTime 结束时间
* @param haveHour 是否有让时间参与计算。默认false。只记天数差。
* @return
*/
public static int subDay(long startTime, long endTime, boolean haveHour) {
long subTime;
if (haveHour) {
subTime = endTime - startTime;
} else {
subTime = getDayStartTime(endTime) - getDayStartTime(startTime);
}
return (int) (subTime / ONE_DATE_TIME);
}
/**
* 与当前日期天数差 endTime-this
* @param endTime 结束时间
* @param haveHour 是否有让时间参与计算。false,只记天数差。
* @return
*/
public static int subDay(long endTime, boolean haveHour) {
return subDay(System.currentTimeMillis(), endTime, haveHour);
}
/**
* 获得中国阴历时间(此方法当前错误,待修改)
* @param solar
* @return
*/
@Deprecated
public static long getLunarCalendar(long solar) {
return solar;
}
/**
* 字符串转换为long,"yyyy-MM-dd"
* @param date
* @return
*/
public static long convert2long(String date) {
if (date != null) {
// 长日期格式
String TIME_FORMAT = "yyyy-MM-dd";
try {
SimpleDateFormat sf = new SimpleDateFormat(TIME_FORMAT);
return sf.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
}
return 0l;
}
/**
* @Title: StringToDate
* @Description: String型转换为date型,"yyyy-MM-dd"
* @param @param date
* @param @return 设定文件
* @return Date 返回类型
* @throws
*/
public static Date StringToDate(String date) {
String TIME_FORMAT = "yyyy-MM-dd";
DateFormat dd = new SimpleDateFormat(TIME_FORMAT);
Date date1 = null;
try {
date1 = dd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date1;
}
/**
* 字符串转换为long,"yyyyMMddHHmmss"
* @param date
* @return
*/
public static long convert3long(String date) {
if (date != null) {
// 长日期格式
String TIME_FORMAT = "yyyyMMddHHmmss";
try {
SimpleDateFormat sf = new SimpleDateFormat(TIME_FORMAT);
return sf.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
}
return 0l;
}
/**
* 字符串转换为long,TIME_FORMAT格式
* @param date
* @return
*/
public static long convert3long(String date, String TIME_FORMAT) {
if (date != null) {
try {
SimpleDateFormat sf = new SimpleDateFormat(TIME_FORMAT);
return sf.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
}
return 0l;
}
/**
* long转换为字符串,"yyyyMMddHHmmss"
* @param date
* @return
*/
public static String convertString(long date) {
if (date != 0) {
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
return formatter.format(calendar.getTime());
}
return null;
}
/**
* long转换为字符串,TIME_FORMAT格式
* @param date
* @return
*/
public static String convertString(long date, String TIME_FORMAT) {
if (date != 0) {
DateFormat formatter = new SimpleDateFormat(TIME_FORMAT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
return formatter.format(calendar.getTime());
}
return null;
}
}