Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert timestamp string to java.util.Date

Tags:

java

I need to convert a timestamp string to java.util.Date. E.g.:

MMDDYYHHMMSS to MM-DD-YY HH-MM-SS

Where MM is month, DD is date, YY is year, HH is hours, MM is minutes and SS is seconds.

like image 742
Manu Avatar asked Feb 23 '10 14:02

Manu


People also ask

How do I manually convert a timestamp to a date?

To easily convert UNIX timestamp to date in the . csv file, do the following: 1. =R2/86400000+DATE(1970,1,1), press Enter key.

Does java Util date have timestamp?

util. Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds.

How do I convert a String to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


2 Answers

You can do it like this:

DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");

but I would strongly recommend that you use Joda Time instead. It's a better date/time library by a long, long way. In particular, the formatters/parsers in Joda Time are thread-safe, so you can reuse them freely and statically; java.text.SimpleDateFormat isn't thread-safe, so you either need to create one per thread or serialize access to it with a synchronized block.

like image 87
Jon Skeet Avatar answered Oct 06 '22 18:10

Jon Skeet


tl;dr

java.time.LocalDateTime.parse(
    "012318123456" ,
    DateTimeFormatter.ofPattern( "MMdduuHHmmss" )
).format( 
    DateTimeFormatter.ofPattern( "MM-dd-uu HH-mm-ss" )
)

01-23-18 12-34-56

java.time

The modern approach uses the java.time classes.

Define a formatting pattern to match your input string.

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

Your two-digit year will be interpreted as being 21st century ( 20xx ).

Parse as a LocalDateTime because your input string lacks any indicator of time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.parse( "012318123456" , f ) ;

ldt.toString(): 2018-01-23T12:34:56

Generate a string in your desired format.

DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "MM-dd-uu HH-mm-ss" ) ;
String output = ldt.format( fOut  );

01-23-18 12-34-56

ISO 8601

Both of your formats are terrible, for multiple reasons.

When serializing date-time values, use the standard ISO 8601 formats whenever possible. They are designed to be practical, easy to parse by machine, easy to read by humans across cultures.

For a date-time time such as yours, the T in the middle separates the date portion from the time-of-day portion.

2018-01-23T12:34:56


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.

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, 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.

like image 3
Basil Bourque Avatar answered Oct 06 '22 19:10

Basil Bourque