package com.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class TimeUtil {
/**
* 取得当天日期,格式 2009-02-11
*
* @return
*/
public static String getToday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 取得当天小时
*
* @return
*/
public static String getTodayHour() {
SimpleDateFormat sdf = new SimpleDateFormat("HH");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 取得当天日期,格式 20090211
*
* @return
*/
public static String getTodayNumber() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 取得当月月份,格式 2009-02
*
* @return
*/
public static String getMonthz() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 给出日期转换成格式 2009-02-11,如果date为空那么返回null
*
* @param date
* @return
*/
public static String getZDDay(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
/**
* 取得当天日期时间,格式 2009-02-11 23:9:21
*
* @return
*/
public static String getTodaytime() {
Calendar cl = new GregorianCalendar();
return getToday() + " " + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":"
+ cl.get(Calendar.SECOND) + " ";
}
/**
* 取得当前时间,格式 23:12:20
*
* @return
*/
public static String getTime() {
Calendar cl = new GregorianCalendar();
return cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " ";
}
/**
* 取得当前小时
*
* @return
*/
public static int getHour() {
Calendar cl = new GregorianCalendar();
return cl.get(Calendar.HOUR_OF_DAY);
}
/**
* 取得当前日期 格式为20090211
*
* @return
*/
public static String getNoFormatToday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 取得当前时间 格式为231611
*
* @return
*/
public static String getNoFormatTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 取得当前年份
*
* @return
*/
public static String getYear() {
return TimeUtil.getNoFormatToday().substring(0, 4);
}
/**
* 取得当前月份
*
* @return
*/
public static String getMonth() {
return TimeUtil.getNoFormatToday().substring(4, 6);
}
/**
* 取得当前日
*
* @return
*/
public static String getDay() {
return TimeUtil.getNoFormatToday().substring(6, 8);
}
/**
* 返回N天前(后的)日期,正数是后的日期,负数是前的日期。例如:2009-02-11 12:12:12
*
* @param number
* @return
*/
public static String getYesterday(int number) {
String strYesterday = "";
Calendar cale = null;
cale = new GregorianCalendar();
cale.add(Calendar.DATE, number);
strYesterday = TimeUtil.getStrByCalendar(cale);
return strYesterday;
}
public static boolean dateFlag(int number, String nowdate) {
boolean flag = false;
try {
String d = getYesterday(-number);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
Date yesterday = sdf.parse(d);
Date nd = sdf.parse(nowdate);
flag = yesterday.before(nd);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public static String getStrByCalendar(Calendar cale) {
return (new SimpleDateFormat("yyyy-MM-dd")).format(cale.getTime());
}
/**
* 日期字符串的格式转换,例如"2009-02-11"转换为2009年2月11日
*
* @param sDate
* @return
*/
public static String getChnDateString(String sDate) {
if (sDate == null) {
return null;
}
sDate = sDate.trim();
if (sDate.length() == 7) {
sDate += "-01";
}
StringTokenizer st = new StringTokenizer(sDate, "-");
int year = 2100;
int month = 0;
int day = 1;
try {
year = Integer.parseInt(st.nextToken());
month = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
} catch (Exception e) {
e.printStackTrace();
}
Calendar cl = new GregorianCalendar(year, month, day);
return cl.get(Calendar.YEAR) + "年" + (cl.get(Calendar.MONTH) + 1) + "月" + cl.get(Calendar.DATE) + "日";
}
/**
* 取得某年某月的最后一天
*
* @param year
* @param month
* @return
*/
public static String getMaxDayOfMonth(int year, int month) {
Calendar cal = new GregorianCalendar(year, month - 1, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("dd");
return sdf.format(cal.getTime());
}
/**
* 取得某年某月的第一天
*
* @param year
* @param month
* @return
*/
public static String getMinDayOfMonth(int year, int month) {
Calendar cal = new GregorianCalendar(year, month - 1, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
/**
* 获取date的下 n个月初始时间(1号0点)
*
* @param date
* @return
*/
public static Date getNextMonthBegin(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
cal.set(Calendar.DATE, 1);
return new Date(cal.getTimeInMillis());
}
/**
* 获取date的当月初始时间(1号0点)
*
* @param date
* @return
*/
public static Date getBeginOfmonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
return new Date(cal.getTimeInMillis());
}
public static int monthsOfDate(Date date1, Date date2) {
int result = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
return result == 0 ? 1 : Math.abs(result);
}
/**
* 取得当天的中文日期,像2006年11月28日 星期二
*
* @return
*/
public static String getChineseToDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E", Locale.CHINESE);
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 取得当天的中文日期,像2006年11月28日 星期二 下午05:06
*
* @return
*/
public static String getChineseToDayTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E a", Locale.CHINESE);
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}
/**
* 根据字符串,取得日期类
*
* @param sDate
* @return
*/
public static Calendar getDate(String sDate) {
if (sDate == null) {
return null;
}
sDate = sDate.trim();
if (sDate.length() == 7) {
sDate += "-01";
}
StringTokenizer st = new StringTokenizer(sDate, "-");
int year = 2100;
int month = 0;
int day = 1;
try {
year = Integer.parseInt(st.nextToken());
month = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
} catch (Exception e) {
e.printStackTrace();
}
return new GregorianCalendar(year, month, day);
}
/**
* 根据日期类取得日期的字符串形式
*
* @param sDate
* @return
*/
public static String getDateString(Calendar sDate) {
if (sDate == null) {
return "";
}
return (new SimpleDateFormat("yyyy-MM-dd")).format(sDate.getTime());
}
/**
* 根据日期类取得日期的字符串形式
*
* @param sDate
* @return
*/
public static String getDateString(Date sDate) {
if (sDate == null) {
return "";
}
return (new SimpleDateFormat("yyyy-MM-dd")).format(sDate.getTime());
}
/**
* 根据日期类取得日期的字符串形式(yyyy-MM-dd HH:mm:ss)
*
* @param sDate
* @return
*/
public static String getDateTimeString(Date sDate) {
if (sDate == null) {
return "";
}
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(sDate.getTime());
}
/**
* 根据日期类取年月的字符串形式
*
* @param sDate
* @return
*/
public static String getYearMonth(Calendar sDate) {
if (sDate == null) {
return "";
}
return (new SimpleDateFormat("yyyy-MM")).format(sDate.getTime());
}
/**
* 比较两个日期类型的字符串,格式为(yyyy-mm-dd) 如果cale1大于cale2,返回1 如果cale1小于cale2,返回-1
* 如果相等,返回0 如果格式错误,返回-2
*
* @param cale1
* @param cale2
* @return
*/
public static int compareCalendar(String cale1, String cale2) {
Calendar calendar1 = getDate(cale1);
Calendar calendar2 = getDate(cale2);
if (calendar1 == null || calendar2 == null) {
return -2;
}
return calendar1.compareTo(calendar2);
}
/**
* 获取当前日期 格式 yyyy-MM-01 00:00:01
*
* @return
*/
public static String getYearMonth() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
return sf.format(new Date()) + "-01 00:00:00";
}
/**
* 获取当前时间 格式:YYYYMMDDhhmmss
*
* @return
*/
public static String getCurrentTime() {
return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
}
public static String getToMonth() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
return sf.format(new Date());
}
public static String getMonthOnly() {
SimpleDateFormat sf = new SimpleDateFormat("MM");
return sf.format(new Date());
}
/**
* 根据参数获取起始时间 年获取的是一月一日零时零分 月获取的是当年的改月的一日 日是零时零分
*
* @return 起始时间的字符串
*/
public static String getBegin(String type, String date) {
switch (type) {
case "Y":
date += "-01-01 00:00:00";
break;
case "M":
date += "-01 00:00:00";
break;
case "D":
date += " 00:00:00";
break;
}
return date;
}
/**
* 获取结束时间
*
* @param type
* @param date
* @return
*/
public static String getEnd(String type, String date) {
SimpleDateFormat sf = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
date = getBegin(type, date);
Calendar len = Calendar.getInstance();
try {
len.setTime(sf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
switch (type) {
case "Y":
len.add(Calendar.YEAR, 1);
break;
case "M":
len.add(Calendar.MONTH, 1);
break;
case "D":
len.add(Calendar.DATE, 1);
break;
}
return sf.format(len.getTime());
}
/**
* 获取两个日期之间的差值 如 4天2时5分3秒
*
* @param from
* @param to
* @return
*/
public static String getDay2Day(Date from, Date to) {
long time = to.getTime() - from.getTime();
String timeStr = "";
long s = time / 1000 % 60;
timeStr = s + "秒";
if (time / 1000 >= 60) {
long m = time / 1000 / 60 % 60;
timeStr = m + "分" + timeStr;
if (time / 1000 / 60 >= 60) {
long h = time / 1000 / 60 / 60 % 24;
timeStr = h + "时" + timeStr;
if (time / 1000 / 60 / 60 >= 24) {
long d = time / 1000 / 60 / 60 / 24;
timeStr = d + "天" + timeStr;
}
}
}
return timeStr;
}
public static String getDay2Day1(Date startTime, Date endTime) {
long time = endTime.getTime() - startTime.getTime();
String timeStr = "";
if (time / 1000 / 60 / 60 >= 24) {
long d = time / 1000 / 60 / 60 / 24;
timeStr = d + "天" + timeStr;
}
return timeStr;
}
public static long getDaySub(String beginDateStr, String endDateStr) {
long day = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate;
Date endDate;
try {
beginDate = format.parse(beginDateStr);
endDate = format.parse(endDateStr);
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
// System.out.println("相隔的天数="+day);
} catch (ParseException e) { // TODO 自动生成 catch 块
e.printStackTrace();
}
return day;
}
/**
* 返回给定日期15号和上个月15号之间得差
*
* @param date
* @return
*/
public static Long getDay2Day1(Date date) {
SimpleDateFormat simpleDateFormatMonth = new SimpleDateFormat("yyyy-MM-15");
SimpleDateFormat simpleDateFormatMonth2 = null;
int month = date.getMonth()+1;
if (month >= 10) {
simpleDateFormatMonth2 = new SimpleDateFormat("yyyy-" + month + "-15");
} else {
simpleDateFormatMonth2 = new SimpleDateFormat("yyyy-0" + month + "-15");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
long diff = sdf.parse(simpleDateFormatMonth.format(date)).getTime()
- sdf.parse(simpleDateFormatMonth2.format(date)).getTime();
long day = diff / 1000 / 24 / 60 / 60;
return day;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 两个日期时间差 int=20
*
* @param date1
* @param date2
* @return
*/
public static Long getDay2Day12(Date date1, Date date2) {
long diff = date1.getTime() - date2.getTime();
long day = diff / 1000 / 24 / 60 / 60;
return day;
}
/**
* 两个日期时间差大于1天直接返回2
*
* @param date1
* @param date2
* @return
*/
public static Long getDay2Day122(Date date1, Date date2) {
if (null != date1 && null != date2) {
long diff = date1.getTime() - date2.getTime();
if (diff > 86400000L) {
return 2L;
} else {
long day = diff / 1000L / 24L / 60L / 60L;
return day;
}
} else {
return 2L;
}
}
/**
* 判断给定时间和1点对比
*
* @param payTime
* @return
*/
public static Boolean isAfterOneTime(Date payTime) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date today = new Date();
Date yesterday = new Date(today.getTime() - 86400000L);
String string = new SimpleDateFormat("yyyy-MM-dd 13:00:00").format(yesterday);
Date yesterDay = df.parse(string);
if (payTime.after(yesterDay)) {
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 取得当前日
*
* @return
*/
public static String getDay(int i) {
return TimeUtil.getTimes(i).substring(8, 10);
}
public static String getTimes(int i){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, i);
return sf.format(c.getTime());
}
public static void main(String[] args) {
Date today = new Date();
System.out.println(today.getTime());
System.out.println(1477058241633L - 86400000L);
Date yesterday = new Date(today.getTime() - 86400000L);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yesterday));
}
}
日期相关
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 通常,我们做iOS App开发过程中,都会经常用到一些常用的功能。例如:启动页,轮播图,下拉框选择,验证码,...
- java.util.Date:Date的每一个实例用于表示一个时间点Date内部维护一个long值,该值是UTC时...