import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import java.text.ParseException;import java.text.SimpleDateFormat;import java.time.Instant;import java.time.LocalDateTime;import java.time.ZoneID;import java.time.temporal.ChronoUnit;import java.util.Calendar;import java.util.Date;/** * @Projectname: DateUtils * @Package: Date * @Description: * @Author: penglaoxi * @CreateDate: 2021/04/30 12:52 */@Componentpublic class DateUtils { private static final Logger logger = LoggerFactory.getLogger(DateUtils.class); public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd"; public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss"; public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss"; /************************************Calendar---工具类*****************************************/ /** * 日期减去一天 * * @param date * @return * @throws ParseException */ public static String dateSubOneDate(String date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(DateUtils.stringToDate(date, DATE_FORMAT_YYYY_MM_DD)); calendar.add(Calendar.DATE, -1);//当前时间减去一天,即一天前的时间 return datetoString(calendar.getTime(), DATE_FORMAT_YYYY_MM_DD); } /** * String 转 Date * * @param date * @param pattern * @return * @throws ParseException */ public static Date stringToDate(String date, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { return sdf.parse(date); } catch (ParseException e) { e.printstacktrace(); } return null; } /** * Date 转 String * * @param date * @param pattern * @return */ public static String datetoString(Date date, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(date); } /************************************LocalDateTime---工具类*****************************************/ //当前日期的下个星期 public static LocalDateTime getNextWeekDay(Date date){ LocalDateTime time = datetoLocalDateTime(date); return time.plus(1, ChronoUnit.WEEKS); } //Date转LocalDateTime public static LocalDateTime datetoLocalDateTime(Date date){ Instant instant = date.toInstant(); ZoneID zone = ZoneID.systemDefault(); return LocalDateTime.ofInstant(instant, zone); } //LocalDateTime转Date public static Date localDateTimetoDate(LocalDateTime localDateTime){ ZoneID zone = ZoneID.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); return Date.from(instant); } //比较两个Date时间先后 public static Boolean isOneAfterTwo(Date one, Date two){ return datetoLocalDateTime(one).isAfter(datetoLocalDateTime(two)); } //检查闰年 public static Boolean isLeadYear(LocalDateTime localDateTime){ return localDateTime.tolocalDate().isLeapYear(); } public static voID main(String[] args) throws InterruptedException { Date one = new Date();// Thread.sleep(3000); //暂停3秒 Date two = new Date(); System.out.println(one + " " + two); System.out.println(isOneAfterTwo(one, two)); System.out.println(isLeadYear(LocalDateTime.Now())); }}
总结 以上是内存溢出为你收集整理的Android工具类篇 日期工具类【DateUtil】全部内容,希望文章能够帮你解决Android工具类篇 日期工具类【DateUtil】所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)