我想在特定日期添加一天.我怎样才能做到这一点?
Date dt = new Date();
Run Code Online (Sandbox Code Playgroud)
现在我想在这个日期添加一天.
Dan*_*ski 459
鉴于Date dt
你有几种可能性:
解决方案1:您可以使用Calendar
该类:
Date dt = new Date();
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();
Run Code Online (Sandbox Code Playgroud)
解决方案2:您应该认真考虑使用Joda-Time库,因为Date
该类存在各种缺点.使用Joda-Time,您可以执行以下操作:
Date dt = new Date();
DateTime dtOrg = new DateTime(dt);
DateTime dtPlusOne = dtOrg.plusDays(1);
Run Code Online (Sandbox Code Playgroud)
解决方案3:使用Java 8,您还可以使用新的JSR 310 API(受Joda-Time启发):
Date dt = new Date();
LocalDateTime.from(dt.toInstant()).plusDays(1);
Run Code Online (Sandbox Code Playgroud)
Mne*_*nth 66
Date today = new Date();
Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));
Run Code Online (Sandbox Code Playgroud)
Date有一个构造函数,使用自UNIX-epoch以来的毫秒数.getTime() - 方法为您提供该值.因此,添加一天的毫秒数就可以了.如果你想定期进行这样的操作,我建议为值定义常量.
重要提示:在所有情况下都不正确.阅读下面的警告评论.
beh*_*zad 16
正如Top答案中所提到的,因为java 8可以这样做:
Date dt = new Date();
LocalDateTime.from(dt.toInstant()).plusDays(1);
Run Code Online (Sandbox Code Playgroud)
但这有时会导致DateTimeException
这样:
java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 2014-11-29T03:20:10.800Z of type java.time.Instant
Run Code Online (Sandbox Code Playgroud)
只需传递时区就可以避免此异常:
LocalDateTime.from(dt.toInstant().atZone(ZoneId.of("UTC"))).plusDays(1);
Run Code Online (Sandbox Code Playgroud)
pla*_*opo 14
我找到了一个向Date对象添加任意时间的简单方法
Date d = new Date(new Date().getTime() + 86400000)
Run Code Online (Sandbox Code Playgroud)
哪里:
86 400 000ms = 1 Day : 24*60*60*1000
3 600 000ms = 1 Hour : 60*60*1000
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 12
LocalDate.of( 2017 , Month.JANUARY , 23 )
.plusDays( 1 )
Run Code Online (Sandbox Code Playgroud)
最好java.util.Date
完全避免上课.但是如果你必须这样做,你可以在麻烦的旧遗留日期时间类和现代java.time类之间进行转换.查看添加到旧类的新方法.
Instant
这个Instant
班级接近等同于Date
,它们都是时间轴上的一个时刻.Instant
解析为纳秒,而Date
毫秒.
Instant instant = myUtilDate.toInstant() ;
Run Code Online (Sandbox Code Playgroud)
您可以为此添加一天,但请记住这一点在UTC中.因此,您不会考虑夏令时等异常情况.指定ChronoUnit
课程的时间单位.
Instant nextDay = instant.plus( 1 , ChronoUnit.DAYS ) ;
Run Code Online (Sandbox Code Playgroud)
ZonedDateTime
如果您想精通时区,请指定a ZoneId
来获取ZonedDateTime
.指定适当的时区名称,格式continent/region
,如America/Montreal
,Africa/Casablanca
或Pacific/Auckland
.切勿使用3-4字母缩写,例如EST
或IST
因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
ZonedDateTime zdtNextDay = zdt.plusDays( 1 ) ;
Run Code Online (Sandbox Code Playgroud)
你也可以代表你要添加的时间跨度,一天,作为一个Period
.
Period p = Period.ofDays( 1 ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ).plus( p ) ;
Run Code Online (Sandbox Code Playgroud)
你可能想要第二天的第一时刻.不要假设这一天从00:00:00开始.夏令时(DST)等异常表示该日可能在另一时间开始,例如01:00:00.让java.time确定该区域中该日期的第一天.
LocalDate today = LocalDate.now( z ) ;
LocalDate tomorrow = today.plus( p ) ;
ZonedDateTime zdt = tomorrow.atStartOfDay( z ) ;
Run Code Online (Sandbox Code Playgroud)
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date
,Calendar
,和SimpleDateFormat
.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
您可以直接与数据库交换java.time对象.使用符合JDBC 4.2或更高版本的JDBC驱动程序.不需要字符串,不需要课程.java.sql.*
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval
,YearWeek
,YearQuarter
,和更多.
更新: Joda-Time库现在处于维护模式.该团队建议迁移到java.time类.我将这段保留完整的历史记录.
该乔达时间 2.3库使这种日期时间的工作变得更加容易.与Java捆绑在一起的java.util.Date类非常麻烦,应该避免使用.
这是一些示例代码.
您的java.util.Date将转换为Joda-Time DateTime对象.与juDate不同,DateTime确实知道其分配的时区.时区是至关重要的,因为在明天增加一天以获得相同的挂钟时间可能意味着在美国的夏令时(DST)情况下进行调整,例如23小时或25小时.如果指定时区,Joda-Time可以进行这种调整.添加一天后,我们将DateTime对象转换回java.util.Date对象.
java.util.Date yourDate = new java.util.Date();
// Generally better to specify your time zone rather than rely on default.
org.joda.time.DateTimeZone timeZone = org.joda.time.DateTimeZone.forID( "America/Los_Angeles" );
DateTime now = new DateTime( yourDate, timeZone );
DateTime tomorrow = now.plusDays( 1 );
java.util.Date tomorrowAsJUDate = tomorrow.toDate();
Run Code Online (Sandbox Code Playgroud)
转储到控制台......
System.out.println( "yourDate: " + yourDate );
System.out.println( "now: " + now );
System.out.println( "tomorrow: " + tomorrow );
System.out.println( "tomorrowAsJUDate: " + tomorrowAsJUDate );
Run Code Online (Sandbox Code Playgroud)
跑的时候......
yourDate: Thu Apr 10 22:57:21 PDT 2014
now: 2014-04-10T22:57:21.535-07:00
tomorrow: 2014-04-11T22:57:21.535-07:00
tomorrowAsJUDate: Fri Apr 11 22:57:21 PDT 2014
Run Code Online (Sandbox Code Playgroud)
小智 10
这将使任何日期增加一个
String untildate="2011-10-08";//can take any date in current format
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Calendar cal = Calendar.getInstance();
cal.setTime( dateFormat.parse(untildate));
cal.add( Calendar.DATE, 1 );
String convertedDate=dateFormat.format(cal.getTime());
System.out.println("Date increase by one.."+convertedDate);
Run Code Online (Sandbox Code Playgroud)
小智 10
你可以在导入org.apache.commons.lang.time.DateUtils之后使用这个方法:
DateUtils.addDays(new Date(), 1));
Run Code Online (Sandbox Code Playgroud)
Java 8 时间 API:
Instant now = Instant.now(); //current date
Instant after= now.plus(Duration.ofDays(300));
Date dateAfter = Date.from(after);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
463535 次 |
最近记录: |