-4

I am new to Java. I am trying to store current date time in long format, like 2019110820000583.

I tried using System.currentTimeMillis() but it doesn't give date and time combined. It gives me result like 1573205716048.

halfer
  • 19,824
  • 17
  • 99
  • 186
Adorable
  • 5
  • 4
  • 1
    Could you please share whatever have you tried? – hagarwal Nov 08 '19 at 09:47
  • 1
    I tried `System.currentTimeMillis()` and the below code.```Calendar date = Calendar.getInstance(); long millisecondsDate = date.getTimeInMillis();``` – Adorable Nov 08 '19 at 09:48
  • 1
    Have you read the documentation about what `System.currentTimeMillis()` or `date.getTimeInMillis()` returns? – f1sh Nov 08 '19 at 09:49
  • 2
    **Read about `java.time`…** You can have a `DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")` there and format every `LocalDateTime` accordingly. – deHaar Nov 08 '19 at 09:52
  • 4
    Ok I found a way myself.```String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());``` It gives result like ***20191108095117*** – Adorable Nov 08 '19 at 09:52
  • 1
    Do you intend UTC or a time zone? – Basil Bourque Nov 08 '19 at 16:56

1 Answers1

1

java.time

Get the current moment in UTC.

OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;

Define a formatting pattern for your desired output.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;

Generate a String with text in your desired format.

String output = odt.format( f ) ;

For a time zone, similar to code above but use ZonedDateTime in place of OffsetDateTime.

ZonedDateTime.now( ZoneId.of( "America/Montreal" ) ) 

ISO 8601

Tip: Generally best to use the standard ISO 8601 formats when serializing date-time values as text. To comply with the “basic” version of ISO 8601:

  • insert a T between the date portion and the time-of-day portion.
  • Append a Z for a moment in UTC. Otherwise append the offset.

So this:

DateTimeFormatter.ofPattern( "uuuuMMdd'T'HHmmssXXXXX" )

See this full example run live at IdeOne.com.

OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMdd'T'HHmmssXXXXX" ) ;
String output = odt.format( f ) ;

odt.toString(): 2019-11-09T04:38:47.972145Z

output: 20191109T043847Z


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.

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

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

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?

Table of which java.time library to use with which version of Java or Android

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