溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java8中的LocalDateTime和Date一些時間操作方法

發(fā)布時間:2020-10-01 06:39:36 來源:腳本之家 閱讀:683 作者:小小華bk 欄目:編程語言

先記錄下jdk8之前的一些幫助方法

判斷time是否在now的n天之內(nèi)

/**
  * 判斷time是否在now的n天之內(nèi)
  * @param time
  * @param now
  * @param n 正數(shù)表示在條件時間n天之后,負數(shù)表示在條件時間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;
  }
 }

判斷某個時間是否是在條件的起始時間與結(jié)束時間之內(nèi)

/**
  * 判斷time是否在from,to之內(nèi)
  *
  * @param time 指定日期
  * @param from 開始日期
  * @param to 結(jié)束日期
  * @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轉(zhuǎn)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;//正數(shù)表示在當前時間之后,負數(shù)表示在當前時間之前
 }

將Date轉(zhuǎn)換成String

private static final String LONG_PATTERN="yyyy-MM-dd HH:mm:ss";
 private static final String SHORT_PATTERN="yyyy-MM-dd";
 
 /**
  * 將日期轉(zhuǎn)換為字符串
  */
 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轉(zhuǎn)換成Date

 private static final String LONG_PATTERN="yyyy-MM-dd HH:mm:ss";
 private static final String SHORT_PATTERN="yyyy-MM-dd";
 
/**
  * 將字符串轉(zhuǎn)為日期
  */
 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);
  //倒計時結(jié)束后的時間
  Date scrap_year = date.getTime();
  System.out.println("三年后時間" + scrap_year);

Jdk8改革

LocalDateTime獲取毫秒數(shù)

//獲取秒數(shù)
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//獲取毫秒數(shù)
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
 
LocalDateTime與String互轉(zhuǎn)
//時間轉(zhuǎn)字符串格式化
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
 String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
 
//字符串轉(zhuǎn)時間
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互轉(zhuǎn)

//將java.util.Date 轉(zhuǎn)換為java8 的java.time.LocalDateTime,默認時區(qū)為東8區(qū)
 public static LocalDateTime dateConvertToLocalDateTime(Date date) {
  return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
 }
 
 
 //將java8 的 java.time.LocalDateTime 轉(zhuǎn)換為 java.util.Date,默認時區(qū)為東8區(qū)
 public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
  return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
 }

將LocalDateTime轉(zhuǎn)為自定義的時間格式的字符串

public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
 return localDateTime.format(formatter);
}

將某時間字符串轉(zhuǎn)為自定義時間格式的LocalDateTime

public static LocalDateTime parseStringToDateTime(String time, String format) {
 DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
 return LocalDateTime.parse(time, df);
}

將long類型的timestamp轉(zhuǎn)為LocalDateTime

public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
 Instant instant = Instant.ofEpochMilli(timestamp);
 ZoneId zone = ZoneId.systemDefault();
 return LocalDateTime.ofInstant(instant, zone);
}

將LocalDateTime轉(zhuǎn)為long類型的timestamp

public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
 ZoneId zone = ZoneId.systemDefault();
 Instant instant = localDateTime.atZone(zone).toInstant();
 return instant.toEpochMilli();
}

總結(jié)

到此這篇關(guān)于Java8中的LocalDateTime和Date一些時間操作方法的文章就介紹到這了,更多相關(guān)java8 localdateTime和date內(nèi)容請搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI