Java如何获取当前日期和时间


本篇博客主要总结Java里面关于获取当前时间的一些方法

System.currentTimeMillis()

获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:

1543105352845 

我们可以将时间戳转化成我们易于理解的格式

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));

则该时间戳对应的时间为:

2018-11-25 at 01:22:12 CET

值得注意的是,此方法会根据我们的系统时间返回当前值,因为世界各地的时区是不一样的。

java.util.Date

在Java中,获取当前日期最简单的方法之一就是直接实例化位于Java包java.util的Date类。

Date date = new Date(); // this object contains the current date value 

上面获取到的日期也可以被format成我们需要的格式,例如:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS");  
System.out.println(formatter.format(date));

Calendar API

Calendar类,专门用于转换特定时刻和日历字段之间的日期和时间。

使用Calendar 获取当前日期和时间非常简单:

Calendar calendar = Calendar.getInstance(); // get current instance of the calendar  

与date一样,我们也可以非常轻松地format这个日期成我们需要的格式

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");  
System.out.println(formatter.format(calendar.getTime()));  

上面代码打印的结果如下:

25-11-2018 00:43:39

Date/Time API
Java 8提供了一个全新的API,用以替换java.util.Date和java.util.Calendar。Date / Time API提供了多个类,帮助我们来完成工作,包括:

LocalDate
LocalTime
LocalDateTime
ZonedDateTime
LocalDate
LocalDate只是一个日期,没有时间。 这意味着我们只能获得当前日期,但没有一天的具体时间。

LocalDate date = LocalDate.now(); // get the current date 

我们可以format它

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");  
System.out.println(date.format(formatter));  

得到的结果只有年月日,例如:

25-11-2018 

LocalTime

LocalTime与LocalDate相反,它只代表一个时间,没有日期。 这意味着我们只能获得当天的当前时间,而不是实际日期:

LocalTime time = LocalTime.now(); // get the current time  

可以按如下方式format

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");  
System.out.println(time.format(formatter));

得到的结果类似如下:

00:55:58  

LocalDateTime

最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个类的组合 - 即日期和时间的值:

LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time  

format的方式也一样

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
System.out.println(dateTime.format(formatter));

得到的日期结果类似于:

25-11-2018 00:57:20  

比较两个日期

SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
Date date1 = dateFormat.parse("2019-09-16");
Date date2 = dateFormat.parse("2020-01-25");

if(date1.before(date2)){
   System.out.println("Date1 时间在 Date2 之前");
} 

if(date1.after(date2)){
   System.out.println("Date1 时间在 Date2 之后");
} 

if(equals.equals(date2)){
   System.out.println("Date1 时间与 Date2 相等");
} 

# compareTo():等于,返回 0,大于参数日期,返回正值,小于参数日期,返回负数
if(equals.compareTo(date2)){
   System.out.println("Date-1 is compareTo Date-2");
}

——————————————————————————————————————-------------------------------------
# 使用java.util.Calendar比较两个Date日期
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);

if(cal1.after(cal2)) {
   System.out.println("Date1 时间在 Date2 之后");
}

if(cal1.before(cal2)) {
   System.out.println("Date1 时间在 Date2 之前");
}

if(cal1.equals(cal2)) {
   System.out.println("Date1 时间与 Date2 相等");
}
--------------------------------------------------------------------------
#  Java 8日期比较
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date1 = LocalDate.of(2009, 12, 31);
LocalDate date2 = LocalDate.of(2019, 1, 31);

if(date1.isAfter(date2)) {
   System.out.println("Date1 时间在 Date2 之后");
}

if(date1.isBefore(date2)) {
   System.out.println("Date1 时间在 Date2 之前");
}

if(date1.isEqual(date2)) {
   System.out.println("Date1 时间与 Date2 相等");
}

时间戳与Date相互转换

// 一、时间戳与Date相互转换
// 时间戳转Date
public static void timestamp2Date() {
	long timeMillis = System.currentTimeMillis();
	Date date = new Date(timeMillis);
}
// Date转时间戳
public static void date2Timestamp() {
	Date date = new Date();
	long timeMillis = date.getTime();
}
// 二、时间戳与LocalDateTime相互转换
// 时间戳转LocalDateTime
public static void timestamp2LocalDateTime() {
	long timeMillis = System.currentTimeMillis();
	ZoneId zoneId = ZoneId.systemDefault();
	LocalDateTime localDateTime = Instant.ofEpochMilli(timeMillis).atZone(zoneId).toLocalDateTime();
}
// LocalDateTime转时间戳
public static void localDateTime2Timestamp() {
	ZoneId zoneId = ZoneId.systemDefault();
	LocalDateTime now = LocalDateTime.now();
	long epochMilli = now.atZone(zoneId).toInstant().toEpochMilli();
}

LocalDateTime常见用法 

// 1、获取当前日期
年月日时分秒:LocalDateTime now=LocalDateTime.now();
年月日:LocalDate today = LocalDate.now();
时分秒:LocalTime time= LocalTime.now();

// 2、获取某一个日期
根据字符串获取: LocalDate date= LocalDate.parse(“2020-01-10”);
取本月第1天:LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2020-01-01
取本月第2天:LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2020-01-02
取本月最后一天:LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2019-12-31
取2020年1月第一个周一:LocalDate firstMonday = LocalDate.parse(“2020-01-01”).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2020-01-06

// 日期加减
LocalDateTime nextnow= now.plusDays(1); //返回LocalDateTime加上指定天数得到的值
now.plusHours(3); //返回LocalDateTime加上指定小时数得到的值
now.plusMinutes(10); //返回LocalDateTime加上指定分钟数得到的值
now.plusMonths(2); //返回LocalDateTime加上指定月数得到的值
now.plusWeeks(1); //返回LocalDateTime加上指定星期数得到的值
now.plusYears(3); //返回LocalDateTime加上指定年数得到的值
now.minusHours(2); //返回LocalDateTime减去指定小时数得到的值
now.minusHours(2); //返回LocalDateTime减去指定小时数得到的值
now.minusMinutes(2); //返回LocalDateTime减去指定分钟数得到的值
now.minusMonths(2); //返回LocalDateTime减去指定月数得到的值
now.minusNanos(2); //返回LocalDateTime减去指定纳秒数得到的值
now.minusSeconds(2); //返回LocalDateTime减去指定秒数得到的值
now.minusWeeks(2); //返回LocalDateTime减去指定星期数得到的值
now.minusYears(2); //返回LocalDateTime减去指定年数得到的值
// 4、2个日期的计算
//相差的小时数:
Duration duration = Duration.between(createAt, now);
long hours = duration.toHours();
//相差的分钟数:
long minutes=duration.toMinutes();

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM