栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java8中的LocalDateTime和Date一些时间操作方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java8中的LocalDateTime和Date一些时间操作方法

先记录下jdk8之前的一些帮助方法

判断time是否在now的n天之内


 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;
  }
 }

判断某个时间是否是在条件的起始时间与结束时间之内


 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内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号