cle*_*tus 46
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, -30);
Date today30 = cal.getTime();
cal.add(Calendar.DAY_OF_MONTH, -60);
Date today60 = cal.getTime();
cal.add(Calendar.DAY_OF_MONTH, -90);
Date today90 = cal.getTime();
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 10
捆绑的java.util.Date和.Calendar类是众所周知的麻烦.避免他们.
下面是一些使用Joda-Time 2.3库的LocalDate类的示例代码.
LocalDate // Represent a date-only value, without time-of-day and without time zone.
.now( // Capture today's date as seen in the wall-clock time used by the people of a particular region (a time zone).
ZoneId.of( "Asia/Tokyo" ) // Specify the desired/expected zone.
) // Returns a `LocalDate` object.
.minus( // Subtract a span-of-time.
Period.ofDays( 30 ) // Represent a span-of-time unattached to the timeline in terms of years-months-days.
) // Returns another `LocalDate` object.
Run Code Online (Sandbox Code Playgroud)
转储到控制台......
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
Run Code Online (Sandbox Code Playgroud)
跑的时候......
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Run Code Online (Sandbox Code Playgroud)
一天的开始和结束取决于您的时区.巴黎新的一天早于蒙特利尔.
默认情况下,LocalDate类使用JVM的默认时区来确定当前日期.或者,您可以传递DateTimeZone对象.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Run Code Online (Sandbox Code Playgroud)
Java 8带来了新的内置java.time包.这些新类旨在取代旧的捆绑java.util.Date/Calendar类.java.time类的灵感来自JSR 310定义的Joda-Time.
在本课题的特定情况下,上面看到的Joda-Time示例代码与java.time完全相同.只有LocalDate陈述会有所不同.
| 归档时间: |
|
| 查看次数: |
46029 次 |
| 最近记录: |