您的位置:首页 > 其它

时间类型的转换

2017-08-30 22:38 183 查看
下面的方法可以得到三天前的具体时间-->
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 getN10ff7owMonthEndDate() {        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(StringdateStr)根据默认格式校验日期
booleanisValidDate(StringdatetStr, String dateFormat)自定义格式校验日期
booleanisValidDateTime(StringdatetimeStr)根据默认格式校验时间
booleanisValidDateTime(StringdatetimeStr, String datetimeFormat)自定义格式校验时间
booleanvalidFormat(Stringstr, String format)格式校验抽象方法
StringgetTodayTime()获得今天具体时间
StringgetTodatDate()获得今天日期
StringgetYesterdayDate()获得昨天日期
StringgetYesterdayTime()获得昨天具体时间
StringgetNowMonth()获取当前月
StringgetLastMonth()获取上一个月
StringgetTodayBeginTime()获取当天的开始时间
StringgetTodayEndTime获取当天的结束时间
StringgetYesterdayBeginTime()获取前一天的开始时间
StringgetYesterdayEndTime()获取前一天的结束时间
StringgetNowMonthBeginTime()获得当前月的第一天开始时间
StringgetNowMonthBeginDate()获得当前月的第一天开始日期
StringgetNowMonthEndTime()获得当前月最后一天结束时间
StringgetNowMonthEndDate()获得当前月最后一天结束日期
StringgetLastMonthBeginTime()获得前一月的第一天开始时间
StringgetLastMonthBeginDate()获得前一月的第一天开始日期
StringgetLastMonthEndTime()获得前一月的最后一天结束时间
StringgetLastMonthEndDate()获得前一月的最后一天结束日期
StringgetAfterDaysTime(IntegerdateLen)获得当前时间后X天之后的结束时间
StringgetNowWeekBeginTime()获得本周开始时间
StringgetNowWeekEndTime()获得本周结束时间
StringgetLastWeekBeginTime()获得上周开始时间
StringgetLastWeekEndTime()获得上周结束时间
StringgetThreeMonthAgoTime()获得3个月前时间
StringgetThreeMonthAgoDate()获得3个月前日期
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Date 时间