Java: Convert String to Timestamp

How to convert string timestamp into timestamp with a specific timezone

Your code using java.time classes:

        ZonedDateTime zdt = ZonedDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTimeFormatter dfGMT = df.withZone(ZoneId.of("GMT"));

String dateString = dfGMT.format(zdt);
System.out.println("DateString: "+dateString);

ZonedDateTime parsedDate = ZonedDateTime.parse(dateString,dfGMT);
System.out.println("ParsedDate: "+ parsedDate);
Timestamp timestamp = Timestamp.from(parsedDate.toInstant());
System.out.println("Zoned Timestamp: "+timestamp);

//ignoring zone info from date string
LocalDateTime ldt = LocalDateTime.from(dfGMT.parse(dateString));
timestamp = Timestamp.valueOf(ldt);
System.out.println("Zone stripped GMT timestamp: "+timestamp);


ZonedDateTime zdt1 = ldt.atZone(ZoneId.of("GMT"));
zdt1 = zdt1.withZoneSameInstant(ZoneId.of("America/Chicago"));
timestamp = Timestamp.valueOf(zdt1.toLocalDateTime());
System.out.println("Zone stripped CST timestamp: "+timestamp);

Output:

DateString: 2021-03-26T09:10:37.537+0000
ParsedDate: 2021-03-26T09:10:37.537Z[GMT]
Zoned Timestamp: 2021-03-26 14:40:37.537
Zone stripped GMT timestamp: 2021-03-26 09:10:37.537
Zone stripped CST timestamp: 2021-03-26 04:10:37.537

How to convert String to TimeStamp

I think you are using just opposite formats if your input format is dd/MM/yyyy:

//date format of input

SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
//convert string to date
d = inputFormat.parse(input);
} catch (ParseException e) {
System.out.println("Date Format Not Supported");
e.printStackTrace();
}

//output date format

SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//convert date to timestamp

return  outputFormat.format(d).toString();

Convert String to Timestamp Java

**Update**
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SomeClass {

public static void main(String[] args) {

System.out.println(convertStringToTimestamp("2015-06-09 11:51:12,708"));
//be consistent here with , and .
System.out.println(convertStringToTimestamp("2015-04-17 11:29:49.564"));
System.out.println();

}

private static Timestamp convertStringToTimestamp(String something) {

SimpleDateFormat dateFormat = null;
if(something.contains(".")) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
}
if(something.contains(",")) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
}
Timestamp timestamp = null;
Date parsedDate;
try {
parsedDate = dateFormat.parse(something);
timestamp = new java.sql.Timestamp(parsedDate.getTime());

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return timestamp;
}

}

How to convert string date to Timestamp in java?

All you need to do is change the string within the java.text.SimpleDateFormat constructor to:
"MM-dd-yyyy HH:mm:ss".

Just use the appropriate letters to build the above string to match your input date.

convert java timestamp string to java timestamp object

Not very elegant, but you could split the input by a dot. That would separate the datetime part from the offset and you can concatenate the desired (and required) sign with the value.

This requires you to know which sign to apply! The code cannot guess it...

Maybe write a method that takes this input String and a sign to be applied as arguments.

Since it seems not possible to parse an unsigned String representation of an offset, you would need something like the following:

public static void main(String[] args) {
String timestamp = "2021010112:12:12.10:00";
// provide a formatter that parses the datetime (the part before the dot)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMddHH:mm:ss");
// split the timestamp String by the dot to separate datetime from offset
String[] split = timestamp.split("\\.");
// parse the datetime part using the formatter defined above
LocalDateTime ldt = LocalDateTime.parse(split[0], dtf);
// and build up an offset using offset part adding a plus sign
ZoneOffset zoneOffset = ZoneOffset.of("+" + split[1]);
// then create an OffsetDateTime from the LocalDateTime and the ZoneOffset
OffsetDateTime result = OffsetDateTime.of(ldt, zoneOffset);
// finally get an Instant from it
Instant instant = result.toInstant(); // <--- INSTANT HERE
// and print the values
System.out.println(result + " = " + instant.toEpochMilli());
}

This outputs

2021-01-01T12:12:12+10:00 = 1609467132000


Related Topics



Leave a reply



Submit