JAVA LocalDateTime,Date,String,Long 日期时间用DateTimeFormatter相互转换以及Calendar的简单使用

时间:2024-10-01 19:46:49
package com.hz.study; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; public class DateTimeUtil { private static final String PATTENT = "yyyy-MM-dd HH:mm:ss"; public static void main(String[] args) { getCurrentTs(); } /** * 获取当前时间戳 */ public static Long getCurrentTs(){ long t1 = System.currentTimeMillis(); Date date = new Date(); long t2 = date.getTime(); System.out.println("t1:"+t1 + " , t2:"+t2); return t1; } //---------date To string,Long,localDateTime,instant /** * date to string */ public static String date2String(Date date){ DateTimeFormatter dtf = DateTimeFormatter.ofPattern(PATTENT); Instant instant = date.toInstant(); ZoneId zone = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); return localDateTime.format(dtf); } /** * localDateTime to date * @param ldt * @return */ private static Date localDateTimeToDate(LocalDateTime ldt) { ZoneId zone = ZoneId.systemDefault(); Instant instant = ldt.atZone(zone).toInstant(); return Date.from(instant); } /** * date to Long */ public static Long date2Long(Date date){ return date.getTime(); } /** * date to LocalDateTime */ public static LocalDateTime date2LocalDateTime(Date date){ ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId); return localDateTime; } /** * date to instant */ public static Instant date2Instant(Date date){ Instant instant = date.toInstant(); return instant; } //--------------string To Long 、Date 、LocalDateTime 、、、、、 /** * string to long */ public static Long string2Long(String time){ DateTimeFormatter dtf = DateTimeFormatter.ofPattern(PATTENT); //time to localDateTime LocalDateTime localDateTime = LocalDateTime.parse(time, dtf); long timestamp = LocalDateTime.from(localDateTime).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); return timestamp; } /** * string to date */ public static Date string2Date(String time){ DateTimeFormatter dtf = DateTimeFormatter.ofPattern(PATTENT); //time to localDateTime LocalDateTime localDateTime = LocalDateTime.parse(time, dtf); //localDateTime to date Date date = localDateTimeToDate(localDateTime); return date; } /** * string to localDateTime */ public static LocalDateTime string2LocalDateTime(String time){ DateTimeFormatter dtf = DateTimeFormatter.ofPattern(PATTENT); LocalDateTime ldt = LocalDateTime.parse(time, dtf); return ldt; } //---------根据date获取年月日,时间加减 Calendar public void getDetail(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hour = cal.get(Calendar.HOUR_OF_DAY); int minute = cal.get(Calendar.MINUTE); int secend = cal.get(Calendar.SECOND); } public void addNumMonth(Date currentDate){ Calendar cal = Calendar.getInstance(); cal.setTime(currentDate); //当前时间加10个月 cal.add(Calendar.MONTH,10); //当前时间加10天 cal.add(Calendar.DAY_OF_MONTH,10); } }