Problem adding minutes with plusMinutes

拟墨画扇 提交于 2019-12-22 04:18:14

问题


I'm trying to add some minutes to a date using plusMinutes, but it just doesn't add anything at all:

Here's the code:

    String currentDate ;
    SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    Date date1= null;
    DateTime dt;

    currentDate ="27/12/2010 11:29" ;

    try {
           date1= myFormat.parse(currentDate);

        } catch (ParseException ex) {

        ex.printStackTrace();

    }
    dt = new DateTime(date1);
    dt.plusMinutes(30);

回答1:


Javadoc says

Returns a copy of this datetime plus the specified number of millis.

so

do something like

dt = new DateTime(date1);
dt = dt.plusMinutes(30);
System.out.println(""+dt);



回答2:


Beauty of joda is that most of their classes are immutable like String in Java. Update operations doesn't change the original object. So plusMinutes(...) returns a new copy of the DateTime with the minutes added which you can assign to a new variable as shown below.

DateTime newDt=dt.plusMinites(30);
System.out.println(newDt);



回答3:


I think you want dt = dt.plusMinutes(30);

plusMinutes returns a calculated dateTime. It does not modify the dateTime it is called on.




回答4:


tl;dr

java.time.LocalDateTime.parse( 
    "27/12/2010 11:29" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm" )
).plusMinutes( 30 )

2010-12-27T11:59

Tip: If you intended this to be a moment, a specific point on the timeline, apply the context of a time zone (ZoneId) to get a ZonedDateTime.

java.time

Your Question uses the troublesome old date-time classes from the earliest versions of Java, and your Question uses the Joda-Time project which is now in maintenance mode. Both have been supplanted by the java.time classes built into Java 8 and later.

Your string input lacks an indicator of time zone or offset-from-UTC. So parse as a java.time.LocalDateTime.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm" ) ;
LocalDateTime ldt = LocalDateTime.parse( "27/12/2010 11:29" , f ) ;

ldt.toString(): 2010-12-27T11:29

Note that you do not have an actual moment, this is not a specific point on the timeline. This is only a vague idea about potential moments along a range of about 26-27 hours. To determine an actual moment, place this in the context of a time zone (or offset): ZonedDateTime zdt = ldt.atZone( ZoneId.of( "Pacific/Auckland" ) ) ;.

Add your minutes.

LocalDateTime later = ldt.plusMinutes( 30 ) ;

later.toString(): 2010-12-27T11:59


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.



来源:https://stackoverflow.com/questions/4540144/problem-adding-minutes-with-plusminutes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!