时间类型的转换

xiaoxiao2021-02-28  194

下面的方法可以得到三天前的具体时间--> 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个月前日期
转载请注明原文地址: https://www.6miu.com/read-17407.html

最新回复(0)