先記錄下jdk8之前的一些幫助方法
目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、坡頭網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
判斷time是否在now的n天之內(nèi)
/** * 判斷time是否在now的n天之內(nèi) * @param time * @param now * @param n 正數(shù)表示在條件時(shí)間n天之后,負(fù)數(shù)表示在條件時(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);//把當(dāng)前時(shí)間賦給日歷 calendar.add(Calendar.DAY_OF_MONTH, n); Date before7days = calendar.getTime(); //得到n前的時(shí)間 if (before7days.getTime() < time.getTime()) { return true; } else { return false; } }
判斷某個(gè)時(shí)間是否是在條件的起始時(shí)間與結(jié)束時(shí)間之內(nèi)
/** * 判斷time是否在from,to之內(nèi) * * @param time 指定日期 * @param from 開(kāi)始日期 * @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; } }
判斷給定時(shí)間與當(dāng)前時(shí)間相差多少天
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();//獲取當(dāng)前時(shí)間 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ù)表示在當(dāng)前時(shí)間之后,負(fù)數(shù)表示在當(dāng)前時(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); //倒計(jì)時(shí)結(jié)束后的時(shí)間 Date scrap_year = date.getTime(); System.out.println("三年后時(shí)間" + 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) //時(shí)間轉(zhuǎn)字符串格式化 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter); //字符串轉(zhuǎn)時(shí)間 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,默認(rèn)時(shí)區(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,默認(rèn)時(shí)區(qū)為東8區(qū) public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.toInstant(ZoneOffset.of("+8"))); }
將LocalDateTime轉(zhuǎn)為自定義的時(shí)間格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter); }
將某時(shí)間字符串轉(zhuǎn)為自定義時(shí)間格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(time, df); }
將long類型的timestamp轉(zhuǎn)為L(zhǎng)ocalDateTime
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一些時(shí)間操作方法的文章就介紹到這了,更多相關(guān)java8 localdateTime和date內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!