Problem adding minutes with plusMinutes

I am trying to add a few minutes to the date using plusMinutes, but just does not add anything:

Here is 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); 
+8
java jodatime
source share
4 answers

Javadoc says

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

So

do something like

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

The beauty of joda is that most of their classes are immutable, like String in Java. Update operations do not modify the original object. So plusMinutes (...) returns a new copy of DateTime using minutes that can be assigned to a new variable, as shown below.

 DateTime newDt=dt.plusMinites(30); System.out.println(newDt); 
+2
source share

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

plusMinutes returns the calculated dateTime value. It does not change the date on which it is called.

+2
source share

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 intend to be a moment, a specific point on the timeline, apply the time zone context ( ZoneId ) to get the ZonedDateTime .

java.time

Your Question uses the nasty old time classes with the earliest versions of Java, and your question uses the Joda-Time project, which is now in maintenance mode. Both were superseded by the java.time classes built into Java 8 and later.

Your string input is missing the time zone indicator or offset-from-UTC . So, java.time.LocalDateTime as 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

Please note that you do not have an actual moment, this is not a specific point on the timeline. This is only a vague idea of ​​potential moments in the range of about 26-27 hours. To determine the actual moment, put this in the context of the 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 supersede the nasty old legacy datetime classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.* Classes needed.

Where to get java.time classes?

  • Java SE 8 , Java SE 9 , and then
    • Built in.
    • Part of the standard Java API with integrated implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
  • Android
    • Later versions of the Android package 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 proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
source share

Source: https://habr.com/ru/post/650465/


All Articles