java中时间_Java中的日期与时间

9246d4c484593c0c57969eaf64dc4352.png

java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含时间),所以他的大部分构造器/方法都已Deprecated,因此就不再推荐使用(如果贸然使用的话,可能会出现性能/安全方面的问题);下面我仅介绍它还剩下的为数不多的几个方法(这些方法的共同点是Date与毫秒值的转换):

构造器

Date(): 在底层调用System.currentTimeMillis()作为日期参数.

Date(long date): 根据指定的long整数(从1970-1-1 00:00:00以来经过的毫秒数)来生成Date对象.

方法

boolean after(Date when): 测试this日期是否在指定日期when之后;

boolean before(Date when): 测试this日期是否在指定日期when之前;

long getTime(): 获取从1979-01-01 00:00:00 到Date对象之间经过的毫秒值;

void setTime(long time): 设置时间,time含义上同.

/**

* Created by jifang on 15/12/30.

*/

public class DateTest {

@Test

public void test() {

Date dateBefore = new Date();

Date dateAfter = new Date(System.currentTimeMillis() + 1);

System.out.println("before: " + dateBefore.getTime());

System.out.println("after: " + dateAfter.getTime());

System.out.println(dateBefore.before(dateAfter));

System.out.println(dateAfter.after(dateBefore));

dateBefore.setTime(System.currentTimeMillis());

System.out.println(dateBefore.getTime());

System.out.println(dateBefore.before(dateAfter));

}

}

日期格式化

完成字符串与日期对象的转化(format/parse)

java.text.DateFormat是一个抽象类, 他提供了如下几个方法获取DateFormat对象.

方法

描述

static DateFormat getDateInstance()

Gets the date formatter with the default formatting style for the default locale.

static DateFormat getDateTimeInstance()

Gets the date/time formatter with the default formatting style for the default locale.

static DateFormat getTimeInstance()

Gets the time formatter with the default formatting style for the default locale.

其实上面三个方法还可以指定日期/时间的样式, 如FULL/LONG/MEDIUM/SHOT, 通过这四个样式参数可以控制生成的格式化字符串. 但由于在我们的实际开发中很少直接用DateFormat类,因此就不对其做过多的介绍.而我们比较常用的是其子类SimpleDateFormat(其实上面几个getXxxInstance方法返回的也是SimpleDateFormat实例)

DateFormat dateFormat = DateFormat.getTimeInstance();

System.out.println(dateFormat.getClass().getName());

SimpleDateFormat

java.text.SimpleDateFormat可以非常灵活的格式化Date, 也可以用于解析各种格式的日期字符串.创建SimpleDateFormat对象时需要传入一个pattern字符串,这个pattern不是正则表达式,而是一个日期模板字符串.

/**

* Created by jifang on 15/12/30.

*/

public class FormatTest {

@Test

public void client() throws ParseException {

DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

// Date -> String

Date date = new Date(System.currentTimeMillis());

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

// String -> Date

String timeString = "2015-12-30 08:53:21";

Date newDate = format.parse(timeString);

System.out.println(newDate);

}

}

在时间日期格式化时, 有下面几个方法是最常用的:

方法

描述

小结

String format(Date date)

Formats a Date into a date/time string.

Date -> String

Date parse(String source)

Parses text from the beginning of the given string to produce a date.

String -> Date

当然, pattern我们还可以根据我们的需求有其他的定制形式:

@Test

public void client() throws ParseException {

DateFormat format = new SimpleDateFormat("yy年MM月dd日 hh时mm分ss秒");

// Date -> String

Date date = new Date(System.currentTimeMillis());

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

// String -> Date

String timeString = "15年12月30日 09时00分29秒";

Date newDate = format.parse(timeString);

System.out.println(newDate);

}

可以看出SimpleDateFormat把日期格式化成怎样的字符串以及能把怎样的字符串解析成Date, 完全取决于创建对象时指定的pattern参数,其他的pattern参数以及SimpleDateFormat的方法可以参考JDK文档.

发表评论

评论已关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java时间操作可以使用Java标准库java.time包来完成。 Java 8之前,可以使用java.util.Datejava.util.Calendar类来操作时间。但是这两个类在设计上存在问题,因此在Java 8引入了新的时间API。 Java 8时间API提供了3个核心类:LocalDate、LocalTime和LocalDateTime。这些类都是不可变的,线程安全的,并且提供了许多方法来操作时间。 以下是一些常见的时间操作: 1. 获取当前时间 可以使用LocalDate、LocalTime或LocalDateTime类的now()方法来获取当前的日期/时间/日期时间。 示例代码: ```java LocalDate currentDate = LocalDate.now(); LocalTime currentTime = LocalTime.now(); LocalDateTime currentDateTime = LocalDateTime.now(); ``` 2. 时间格式化 可以使用DateTimeFormatter类来格式化时间。 示例代码: ```java LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = currentDateTime.format(formatter); ``` 3. 时间比较 可以使用Java 8的新方法来比较时间。例如,使用LocalDate的isAfter()、isBefore()、isEqual()方法来比较两个日期之间的关系。 示例代码: ```java LocalDate date1 = LocalDate.of(2022, 1, 1); LocalDate date2 = LocalDate.of(2022, 1, 2); if (date1.isBefore(date2)) { System.out.println("date1 is before date2"); } else if (date1.isAfter(date2)) { System.out.println("date1 is after date2"); } else { System.out.println("date1 is equal to date2"); } ``` 4. 时间加减 可以使用Java 8的新方法来加减时间。例如,使用LocalDate的plus()、minus()方法来增加或减少指定的天数。 示例代码: ```java LocalDate currentDate = LocalDate.now(); LocalDate nextDay = currentDate.plusDays(1); LocalDate lastDay = currentDate.minusDays(1); ``` 以上是Java的一些时间操作,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值