java时间当天、昨天、本周、本月的工具类

java时间当天、昨天、本周、本月的工具类-上流阁

java利用Calendar和SimpleDateFormat返回时间当天、昨天、本周、本月的工具类字符串,具体实现工具类如下:

import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DataTimeUtils {

private final static String timeformat = "yyyy-MM-dd HH:mm:ss";

/**
* 今天开始时间
*
* @return
*/
public static String getTodayStartTime() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return sdf.format(todayStart.getTime());
}

/**
* 今天结束时间
*
* @return
*/
public static String getTodayEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 24);
todayEnd.set(Calendar.MINUTE, 0);
todayEnd.set(Calendar.SECOND, 0);
todayEnd.set(Calendar.MILLISECOND, 0);
return sdf.format(todayEnd.getTime());
}

/**
* 昨天开始时间
*
* @return
*/
public static String getYesterdayStartTime() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
Calendar todayStart = Calendar.getInstance();
todayStart.add(Calendar.DATE, -1);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return sdf.format(todayStart.getTime());
}

/**
* 昨天结束时间
*
* @return
*/
public static String getYesterdayEndTime() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
Calendar todayEnd = Calendar.getInstance();
todayEnd.add(Calendar.DATE, -1);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return sdf.format(todayEnd.getTime());
}

// 获得本周一0点时间
public static String getTimesWeekmorning() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
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 sdf.format(cal.getTime());
}

// 获得本周日24点时间
public static String getTimesWeeknight() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
cal1.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.setTime(cal1.getTime());
cal.add(Calendar.DAY_OF_WEEK, 7);
return sdf.format(cal.getTime());
}

// 获得本月第一天0点时间
public static String getTimesMonthmorning() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
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 sdf.format(cal.getTime());
}

// 获得本月最后一天24点时间
public static String getTimesMonthnight() {
SimpleDateFormat sdf = new SimpleDateFormat(timeformat);
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 sdf.format(cal.getTime());
}

public static void main(String[] args) {

System.out.println("当天0点时间:"+ getTodayStartTime());
System.out.println("当天24点时间:"+ getTodayEndTime());
System.out.println("昨天0点时间:"+ getYesterdayStartTime());
System.out.println("昨天24点时间:"+getYesterdayEndTime());
System.out.println("本周周一0点时间:"+ getTimesWeekmorning());
System.out.println("本周周日24点时间:"+getTimesWeeknight());
System.out.println("本月初0点时间:"+ getTimesMonthmorning());
System.out.println("本月未24点时间:"+ getTimesMonthnight());
}

}

 

 

*文章为作者独立观点,不代表上流阁立场
本文由 江风成 授权 上流阁 发表,并经上流阁编辑。转载此文章须经作者同意,并请附上出处(上流阁)及本页链接。原文链接https://www.o6c.com/java/2017/02/22/906.html
发表评论

坐等沙发
相关文章
html用css实现关闭文本框输入法切换输入法
html用css实现关闭文本框输入法切换输入法
Mysql索引的最左前缀原则
Mysql索引的最左前缀原则
Java技术中Mysql之Mybaitis中IN的函数拼接方法
Java技术中Mysql之Mybaitis中IN的函数拼…
Web前端html5网页three.js、earth会转动的地球
Web前端html5网页three.js、earth会转动…
java和安卓中对Map的key值和value值进行升序降序排序干货
java和安卓中对Map的key值和value值进行…
java用户metadata-extractor读取照片信息错误Exception in thread “main” java.lang.NoClassDefFoundError: com/adobe/xmp/XMPException
java用户metadata-extractor读取照片信…
javaweb开发程序员php开发,微信开发。接受定制开发

最新评论