时间类型的转换

下面的方法可以得到三天前的具体时间-->

	Date start=new Date();//取时间
        
	Calendar calendar = new GregorianCalendar();
        
	calendar.setTime(start);
        
	calendar.add(calendar.DATE,-3);//把日期往前推3天
        
	start=calendar.getTime();

	我们的用Gson转化的Date类型数据到前台会转化为一个时间戳:
	new Gson().toJson(对象索引)

也可以使用普遍的转换方法-->
	
	Date date = new Date();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String todayTime = formate.format(date);

下面介绍一个神奇的时间工具类:

package com.jd.ecc.commons.lib.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateFormatUtil {
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String YEAR_MONTH_FORMAT = "yyyy-MM";


    public DateFormatUtil() {
    }


    public static boolean isValidDate(String dateStr) {
        return validFormat(dateStr, "yyyy-MM-dd");
    }


    public static boolean isValidDate(String datetStr, String dateFormat) {
        return validFormat(datetStr, dateFormat);
    }


    public static boolean isValidDateTime(String datetimeStr) {
        return validFormat(datetimeStr, "yyyy-MM-dd HH:mm:ss");
    }


    public static boolean isValidDateTime(String datetimeStr, String datetimeFormat) {
        return validFormat(datetimeStr, datetimeFormat);
    }


    public static boolean validFormat(String str, String format) {
        boolean convertSuccess = true;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);


        try {
            simpleDateFormat.setLenient(false);
            Date e = simpleDateFormat.parse(str);
            Date maxDate = simpleDateFormat.parse("9999-12-31");
            if(e.after(maxDate)) {
                convertSuccess = false;
            }
        } catch (ParseException var6) {
            convertSuccess = false;
        }


        return convertSuccess;
    }


    public static String getTodayTime() {
        Date date = new Date();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String todayTime = formate.format(date);
        return todayTime;
    }


    public static String getTodatDate() {
        Date date = new Date();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        String todayDate = formate.format(date);
        return todayDate;
    }


    public static String getYesterdayDate() {
        Date date = new Date();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, -1);
        date = calendar.getTime();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        String yesterdayDate = formate.format(date);
        return yesterdayDate;
    }


    public static String getYesterdayTime() {
        Date date = new Date();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, -1);
        date = calendar.getTime();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String yesterdayTime = formate.format(date);
        return yesterdayTime;
    }


    public static String getNowMonth() {
        Date d = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String nowMonth = df.format(d);
        System.out.println(nowMonth);
        return nowMonth;
    }


    public static String getLastMonth() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        int validMonth = cd.get(2) - 1;
        cd.set(2, validMonth);
        Date dt = cd.getTime();
        String lastMonth = df.format(dt);
        return lastMonth;
    }


    public static String getTodayBeginTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cd.set(11, 0);
        cd.set(12, 0);
        cd.set(13, 0);
        Date dt = cd.getTime();
        String todayBeginTime = df.format(dt);
        return todayBeginTime;
    }


    public static String getTodayEndTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cd.set(11, 23);
        cd.set(12, 59);
        cd.set(13, 59);
        Date dt = cd.getTime();
        String todayEndTime = df.format(dt);
        return todayEndTime;
    }


    public static String getYesterdayBeginTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int yesterday = cd.get(5) - 1;
        cd.set(5, yesterday);
        cd.set(11, 0);
        cd.set(12, 0);
        cd.set(13, 0);
        Date dt = cd.getTime();
        String yesterdayBeginTime = df.format(dt);
        return yesterdayBeginTime;
    }


    public static String getYesterdayEndTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int yesterday = cd.get(5) - 1;
        cd.set(5, yesterday);
        cd.set(11, 23);
        cd.set(12, 59);
        cd.set(13, 59);
        Date dt = cd.getTime();
        String yesterdayEndTime = df.format(dt);
        return yesterdayEndTime;
    }


    public static String getNowMonthBeginTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cd.add(2, 0);
        cd.set(5, 1);
        cd.set(11, 0);
        cd.set(12, 0);
        cd.set(13, 0);
        Date dt = cd.getTime();
        String nowMonthBeginTime = df.format(dt);
        return nowMonthBeginTime;
    }


    public static String getNowMonthBeginDate() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cd.add(2, 0);
        cd.set(5, 1);
        Date dt = cd.getTime();
        String nowMonthBeginDate = df.format(dt);
        return nowMonthBeginDate;
    }


    public static String getNowMonthEndTime() {
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ca.set(5, ca.getActualMaximum(5));
        ca.set(11, 23);
        ca.set(12, 59);
        ca.set(13, 59);
        String nowMonthEndTime = df.format(ca.getTime());
        return nowMonthEndTime;
    }


    public static String getNowMonthEndDate() {
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        ca.set(5, ca.getActualMaximum(5));
        String nowMonthEndDate = df.format(ca.getTime());
        return nowMonthEndDate;
    }


    public static String getLastMonthBeginTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cd.add(2, -1);
        cd.set(5, 1);
        cd.set(11, 0);
        cd.set(12, 0);
        cd.set(13, 0);
        Date dt = cd.getTime();
        String lastMonthBeginTime = df.format(dt);
        return lastMonthBeginTime;
    }


    public static String getLastMonthBeginDate() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cd.add(2, -1);
        cd.set(5, 1);
        Date dt = cd.getTime();
        String lastMonthBeginDate = df.format(dt);
        return lastMonthBeginDate;
    }


    public static String getLastMonthEndTime() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cd.set(5, 0);
        cd.set(11, 23);
        cd.set(12, 59);
        cd.set(13, 59);
        Date dt = cd.getTime();
        String lastMonthEndTime = df.format(dt);
        return lastMonthEndTime;
    }


    public static String getLastMonthEndDate() {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cd.set(5, 0);
        Date dt = cd.getTime();
        String lastMonthEndDate = df.format(dt);
        return lastMonthEndDate;
    }


    public static String getAfterDaysTime(Integer dateLen) {
        GregorianCalendar cd = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cd.add(5, dateLen.intValue());
        cd.set(11, 23);
        cd.set(12, 59);
        cd.set(13, 59);
        Date dt = cd.getTime();
        String afterThirtyDaysDate = df.format(dt);
        return afterThirtyDaysDate;
    }


    public static String getNowWeekBeginTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cds = Calendar.getInstance();
        cds.setFirstDayOfWeek(2);
        cds.set(11, 0);
        cds.set(12, 0);
        cds.set(13, 0);
        cds.set(7, 2);
        String thisweekstart = df.format(cds.getTime());
        return thisweekstart;
    }


    public static String getNowWeekEndTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cde = Calendar.getInstance();
        cde.setFirstDayOfWeek(2);
        cde.set(11, 23);
        cde.set(12, 59);
        cde.set(13, 59);
        cde.set(7, 1);
        String thisweekend = df.format(cde.getTime());
        return thisweekend;
    }


    public static String getLastWeekBeginTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cdl = Calendar.getInstance();
        cdl.setFirstDayOfWeek(2);
        cdl.add(4, -1);
        cdl.set(7, 1);
        cdl.set(11, 0);
        cdl.set(12, 0);
        cdl.set(13, 0);
        cdl.set(7, 2);
        String lastweekstart = df.format(cdl.getTime());
        return lastweekstart;
    }


    public static String getLastWeekEndTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cdle = Calendar.getInstance();
        cdle.setFirstDayOfWeek(2);
        cdle.add(4, -1);
        cdle.set(7, 1);
        cdle.set(11, 23);
        cdle.set(12, 59);
        cdle.set(13, 59);
        String lastweekend = df.format(cdle.getTime());
        return lastweekend;
    }


    public static String getThreeMonthAgoTime() {
        new Date();
        Calendar cald = Calendar.getInstance();
        cald.add(2, -3);
        Date date = cald.getTime();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String threeMonthAgoTime = df.format(date);
        return threeMonthAgoTime;
    }


    public static String getThreeMonthAgoDate() {
        new Date();
        Calendar cald = Calendar.getInstance();
        cald.add(2, -3);
        Date date = cald.getTime();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String threeMonthAgoDate = df.format(date);
        return threeMonthAgoDate;
    }
}
功能简介-->
  
  
方法返回类型
方法名称
方法含义
方法返回类型
方法名称
方法含义
booleanisValidDate(String dateStr)根据默认格式校验日期
booleanisValidDate(String datetStr, String dateFormat)自定义格式校验日期
booleanisValidDateTime(String datetimeStr)根据默认格式校验时间
booleanisValidDateTime(String datetimeStr, String datetimeFormat)自定义格式校验时间
booleanvalidFormat(String str, String format)格式校验抽象方法
StringgetTodayTime()获得今天具体时间
StringgetTodatDate()获得今天日期
StringgetYesterdayDate()获得昨天日期
StringgetYesterdayTime()获得昨天具体时间
StringgetNowMonth()获取当前月
StringgetLastMonth()获取上一个月
StringgetTodayBeginTime()获取当天的开始时间
StringgetTodayEndTime获取当天的结束时间
StringgetYesterdayBeginTime()获取前一天的开始时间
StringgetYesterdayEndTime()获取前一天的结束时间
StringgetNowMonthBeginTime()获得当前月的第一天开始时间
StringgetNowMonthBeginDate()获得当前月的第一天开始日期
StringgetNowMonthEndTime()获得当前月最后一天结束时间
StringgetNowMonthEndDate()获得当前月最后一天结束日期
StringgetLastMonthBeginTime()获得前一月的第一天开始时间
StringgetLastMonthBeginDate()获得前一月的第一天开始日期
StringgetLastMonthEndTime()获得前一月的最后一天结束时间
StringgetLastMonthEndDate()获得前一月的最后一天结束日期
StringgetAfterDaysTime(Integer dateLen)获得当前时间后X天之后的结束时间
StringgetNowWeekBeginTime()获得本周开始时间
StringgetNowWeekEndTime()获得本周结束时间
StringgetLastWeekBeginTime()获得上周开始时间
StringgetLastWeekEndTime()获得上周结束时间
StringgetThreeMonthAgoTime()获得3个月前时间
StringgetThreeMonthAgoDate()获得3个月前日期
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值