先記錄下jdk8之前的一些幫助方法
判斷time是否在now的n天之內
/** * 判斷time是否在now的n天之內 * @param time * @param now * @param n 正數表示在條件時間n天之后,負數表示在條件時間n天之前 * @return */ public static boolean belongDate(Date time, Date now, int n) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); //得到日歷 calendar.setTime(now);//把當前時間賦給日歷 calendar.add(Calendar.DAY_OF_MONTH, n); Date before7days = calendar.getTime(); //得到n前的時間 if (before7days.getTime() < time.getTime()) { return true; } else { return false; } }
判斷某個時間是否是在條件的起始時間與結束時間之內
/** * 判斷time是否在from,to之內 * * @param time 指定日期 * @param from 開始日期 * @param to 結束日期 * @return */ public static boolean belongCalendar(Date time, Date from, Date to) { Calendar date = Calendar.getInstance(); date.setTime(time); Calendar after = Calendar.getInstance(); after.setTime(from); Calendar before = Calendar.getInstance(); before.setTime(to); if (date.after(after) && date.before(before)) { return true; } else { return false; } }
判斷給定時間與當前時間相差多少天
public static long getDistanceDays(String date) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); long days = 0; try { Date time = df.parse(date);//String轉Date Date now = new Date();//獲取當前時間 long time1 = time.getTime(); long time2 = now.getTime(); long diff = time1 - time2; days = diff / (1000 * 60 * 60 * 24); } catch (Exception e) { e.printStackTrace(); } return days;//正數表示在當前時間之后,負數表示在當前時間之前 }
將Date轉換成String
private static final String LONG_PATTERN="yyyy-MM-dd HH:mm:ss"; private static final String SHORT_PATTERN="yyyy-MM-dd"; /** * 將日期轉換為字符串 */ public static String parse( Date d, String pattern){ DateFormat df=null; if( pattern!=null && !"".equals(pattern) ){ df=new SimpleDateFormat(pattern); }else{ df=new SimpleDateFormat(LONG_PATTERN); } return df.format( d ); }
將String轉換成Date
private static final String LONG_PATTERN="yyyy-MM-dd HH:mm:ss"; private static final String SHORT_PATTERN="yyyy-MM-dd"; /** * 將字符串轉為日期 */ public static Date parseStringToDate(String str, String pattern){ DateFormat df = null; if( pattern!=null && !"".equals(pattern) ){ df=new SimpleDateFormat( pattern ); }else{ df=new SimpleDateFormat( LONG_PATTERN ); } Date d=null; try { d=df.parse(str); } catch (ParseException e) { e.printStackTrace(); } return d; }
獲取指定年后的日期(例如三年后的日期)
Calendar date = Calendar.getInstance(); date.setTime(new Date()); date.add(Calendar.YEAR, +3); //倒計時結束后的時間 Date scrap_year = date.getTime(); System.out.println("三年后時間" + scrap_year);
Jdk8改革
LocalDateTime獲取毫秒數
//獲取秒數 Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); //獲取毫秒數 Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); LocalDateTime與String互轉 //時間轉字符串格式化 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter); //字符串轉時間 String dateTimeStr = "2018-07-28 14:11:15"; DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
Date與LocalDateTime互轉
//將java.util.Date 轉換為java8 的java.time.LocalDateTime,默認時區為東8區 public static LocalDateTime dateConvertToLocalDateTime(Date date) { return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime(); } //將java8 的 java.time.LocalDateTime 轉換為 java.util.Date,默認時區為東8區 public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.toInstant(ZoneOffset.of("+8"))); }
將LocalDateTime轉為自定義的時間格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter); }
將某時間字符串轉為自定義時間格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(time, df); }
將long類型的timestamp轉為LocalDateTime
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); ZoneId zone = ZoneId.systemDefault(); return LocalDateTime.ofInstant(instant, zone); }
將LocalDateTime轉為long類型的timestamp
public static long getTimestampOfDateTime(LocalDateTime localDateTime) { ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); return instant.toEpochMilli(); }
總結
到此這篇關于Java8中的LocalDateTime和Date一些時間操作方法的文章就介紹到這了,更多相關java8 localdateTime和date內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。