时间戳
1 1.LocalDate转Date 2 3 LocalDate nowLocalDate = LocalDate.now(); 4 Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant()); 5 6 2.LocalDateTime转Date 7 8 LocalDateTime localDateTime = LocalDateTime.now(); 9 Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant()); 10 11 3.Date转LocalDateTime(LocalDate) 12 13 Date date =newDate(); 14 LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime(); 15 LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate(); 16 17 4.LocalDate转时间戳 18 19 LocalDate localDate = LocalDate.now(); 20 longtimestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli(); 21 22 5.LocalDateTime转时间戳 23 24 LocalDateTime localDateTime = LocalDateTime.now(); 25 longtimestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); 26 27 6.时间戳转LocalDateTime(LocalDate) 28 29 longtimestamp = System.currentTimeMillis(); 30 LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate(); 31 LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
获取标准时间
1 package com.lv.study.pm.first; 2 3 public class TestMil { 4 5 public static void main(String[] args) { 6 7 8 //想要获取我们标准时刻;现在距离1970年1.1 00:00:00多少毫秒 9 //java获取标准时刻 10 11 //1 获取时刻 12 System.out.println(System.currentTimeMillis()); 13 long it=System.currentTimeMillis(); 14 15 //2 距离标准时刻过了多少秒 16 System.out.println(it/1000); 17 18 //3 过了多少个分钟 19 System.out.println(it/1000/60); 20 21 //4 过了多少小时 22 System.out.println(it/1000/60/60); 23 24 //5 过了多少天 25 System.out.println(it/1000/60/60/24); 26 27 //6 过了多少年 28 System.out.println(it/1000/60/60/24/365); 29 30 } 31 32 }
方法2
1 package com.lv.study.pm.first; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class TestSimpleDateFormat { 7 8 public static void main(String[] args) { 9 10 test2(); 11 12 } 13 14 public static void test2(){ 15 //只要符合 yyyy MM dd HH mm ss的规则就可以的 16 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 17 18 System.out.println(sdf.format(new Date())); 19 } 20 21 public static void test1(){ 22 23 //针对java.util.Date 做一个格式化输出 24 25 //以指定的格式输出日期 26 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 27 28 //new Date()当前日期 29 String sdate=sdf.format(new Date()); 30 31 System.out.println(sdate); 32 } 33 34 }
方法3
1 package com.lv.study.pm.second; 2 3 import java.time.LocalDate; 4 import java.time.LocalDateTime; 5 import java.time.LocalTime; 6 7 public class TestLocalDate { 8 9 public static void main(String[] args) { 10 11 //只负责日期,不负责时间只负责年月日 12 LocalDate ld=LocalDate.now(); 13 14 System.out.println(ld.getYear()); 15 System.out.println(ld.getMonth()); 16 System.out.println(ld.getMonthValue()); 17 System.out.println(ld.getDayOfMonth()); 18 19 //只负责时间不负责日期 20 LocalTime it=LocalTime.now(); 21 System.out.println(it.getHour()); 22 23 24 //都负责 25 LocalDateTime ldt=LocalDateTime.now(); 26 System.out.println(ldt.getDayOfYear()); 27 28 } 29 }