java里 currenttime_java 获取当前时间LocalDateTime currentTimeMillis java.util.Date

总结java里面关于获取当前时间的一些方法

System.currentTimeMillis()

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

我们可以将时间戳转化成我们易于理解的格式java;">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");

System.out.println(formatter.format(date));

Calendar API

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

使用Calendar 获取当前日期和时间非常简单:Calendar calendar = Calendar.getInstance(); // gets 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(); // gets 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(); // gets the current time

可以按如下方式formatDateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

System.out.println(time.format(formatter));

得到的结果类似如下:00:55:58

LocalDateTime

最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个累的组合 - 即日期和时间的值:LocalDateTime dateTime = LocalDateTime.now(); // gets 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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java获取当前时间可以使用`java.util.Date`和`java.time.LocalDateTime`两个类。 方式一:使用java.util.Date类 ```java import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(); System.out.println(date); } } ``` 方式二:使用java.time.LocalDateTime类 ```java import java.time.LocalDateTime; public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println(now); } } ``` 这两种方式都可以获取当前时间,其中`java.time.LocalDateTime`是Java 8之后新增的时间类。 ### 回答2: Java获取当前时间有多种方法,下面我将介绍两种常用的方式: 1. 使用System类的currentTimeMillis方法获取当前时间的毫秒数,然后通过Date类将其转换为日期格式。 ``` long currentTimeMillis = System.currentTimeMillis(); // 获取当前时间的毫秒数 Date date = new Date(currentTimeMillis); // 将毫秒数转换为日期格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式 String currentTime = sdf.format(date); // 将日期格式化为字符串 System.out.println("当前时间:" + currentTime); ``` 2. 使用Calendar类来获取当前时间的年、月、日、时、分、秒等信息。 ``` Calendar calendar = Calendar.getInstance(); // 获取当前时间的Calendar实例 int year = calendar.get(Calendar.YEAR); // 获取年份 int month = calendar.get(Calendar.MONTH) + 1; // 获取月份(月份从0开始,需要加1) int day = calendar.get(Calendar.DAY_OF_MONTH); // 获取日期 int hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取小时 int minute = calendar.get(Calendar.MINUTE); // 获取分钟 int second = calendar.get(Calendar.SECOND); // 获取秒钟 System.out.println("当前时间:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second); ``` 以上两种方法都可以获取到当前时间,根据实际需求选择合适的方式进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值