1

How to add 30 seconds to the datetime in the format yyyyMMddHHmmss in java

SO far I have done this

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Date d;
        d = df.parse(requestDate);
        System.out.println(" date d "+d);
        Long time = d.getTime();
        time +=30;
        Date d2 = new Date(time);
        System.out.println(" date d2 "+d2);

And the output I am getting is not correct and both are same time . The output I am getting is

 date d Wed Apr 30 19:32:47 IST 2014
 date d2 Wed Apr 30 19:32:47 IST 2014
Senchu Thomas
  • 518
  • 3
  • 9
  • 24
  • 1
    `Date` and `SimpleDateFormat` are long outdated and the latter in particular notoriously troublesome. I suggest you don’t use them and use [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) for your task instead. – Ole V.V. Feb 24 '18 at 09:34

4 Answers4

2

tl;dr

LocalDateTime.parse( 
    "20180123123456" , 
    DateTimeFormatter.ofPattern( “uuuuMMddHHmmss” ) 
).plusSeconds( 5 ) 

java.time

Use modern java.time classes rather than terrible old legacy classes.

Parse as a LocalDateTime as your input apparently lacks any indicator of offset or time zone.

DateTimeFormatter f = DateTimeFormatter.ofPattern( “uuuuMMddHHmmss” ) ;
LocalDateTime ldt = LocalDateTime.parse( “20180123123456” , f ) ;
Duration d = Duration.ofSeconds( 30 ) ;  // Represent a span of time unattached to the timeline. 
LocalDateTime later = ldt.plus( d ) ;
String output = later.format( f ) ;  // Generate a String representing the value of this object.

Tip: Use ISO 8601 standard formats instead.

Search Stack Overflow for more info. This has been covered many times already.


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.

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

Where to obtain the java.time classes?

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

You have the correct idea, but made a small mistake. getTime gives the time in milliseconds, not seconds. So you need to add 30000.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • You’re being very friendly. Using the long outdated `Date` and the notoriously troublesome `SimpleDateFormat` and hand-doing the calculation rather than relying on a standard class for the math — it may qualify as *one* correct idea depending on interpretation, certainly not *the* correct idea as in the only correct idea. – Ole V.V. Feb 24 '18 at 14:42
0

You need add 30000 millisecond(30 sec)where 1000 Millisecond means 1 Second

 SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    Date d;
    d = df.parse(requestDate);
    System.out.println(" date d "+d);
    Long time = d.getTime();
    time +=30000;//Here Change
    Date d2 = new Date(time);
    System.out.println(" date d2 "+d2);
Bhavya Gandhi
  • 507
  • 3
  • 10
0

Please read the Date.getTime() doc: Returns the number of milliseconds...

    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    Date d;
    d = df.parse(requestDate);
    System.out.println(" date d "+d);
    Long time = d.getTime();
    time +=30000; //<---milliseconds
    Date d2 = new Date(time);
    System.out.println(" date d2 "+d2);
Idan
  • 11
  • 3