Convert Yyyy-Mm-Dd Hh:Mm:Ss.Sss Zzz Format Strin to Date Object

How we can convert yyyy-MM-dd-HH.mm.ss.SSSSSS to yyyy-MM-dd'T'HH:mm:ss.SSSz in Java?

Java 8+

You could also use the newer Time API in Java 8, something like...

String formatIn = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
String formatOut = "yyyy-MM-dd'T'HH:mm:ss.SSSz";

String valueIn = "2016-01-19-09.55.00.000000";

LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
System.out.println("< " + ldt);

ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
String out = DateTimeFormatter.ofPattern(formatOut).format(zdt);
System.out.println("> " + out);

Which outputs...

< 2016-01-19T09:55
> 2016-01-19T09:55:00.000AEDT

This makes you far more responsible for managing the time zones which might be a better solution generally

And because converting between time zones in the Java 8 API gives me a headache (lack of experience :P)

LocalDateTime ldt = LocalDateTime.parse(valueIn, DateTimeFormatter.ofPattern(formatIn));
System.out.println("< " + ldt);

ZonedDateTime here = ldt.atZone(ZoneId.systemDefault());
System.out.println("here " + here);

ZonedDateTime there = here.withZoneSameInstant(ZoneId.of("GMT"));
System.out.println("there " + there);
String out = DateTimeFormatter.ofPattern(formatOut).format(there);
System.out.println("> " + out);

Which outputs...

< 2016-01-19T09:55
here 2016-01-19T09:55+11:00[Australia/Sydney]
there 2016-01-18T22:55Z[GMT]
> 2016-01-18T22:55:00.000GMT

FYI: I think your input is using nano/micro seconds and not milliseconds (there's only 1000 milliseconds in a second). SimpleDateFormat does not support nano/micro seconds, but DateTimeFormatter does, you'd have to use the n pattern, yyyy-MM-dd-HH.mm.ss.nnnnnn for example

Java 7 and below

The basic answer is, use a SimpleDateFormat....

String formatIn = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
String formatOut = "yyyy-MM-dd'T'HH:mm:ss.SSSz";

String valueIn = "2016-01-19-09.55.00.000000";

SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);

Date dateIn = in.parse(valueIn);
System.out.println("< " + dateIn);

String valueOut = out.format(dateIn);
System.out.println("> " + valueOut);

Which outputs...

< Tue Jan 19 09:55:00 AEDT 2016
> 2016-01-19T09:55:00.000AEDT

The problem here is, you could be converting across different time zones, which case, you could use something like...

in.setTimeZone(TimeZone.getTimeZone("GMT"));
dateIn = in.parse(valueIn);
System.out.println("< " + dateIn);

out.setTimeZone(TimeZone.getTimeZone("GMT"));
valueOut = out.format(dateIn);
System.out.println("> " + valueOut);

which outputs

< Tue Jan 19 20:55:00 AEDT 2016
> 2016-01-19T09:55:00.000GMT

or a combination of, if you want to covert to a different time zone.

But, personally, I'd use Joda-Time, but that's me

Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(time);
String formattedTime = output.format(d);

This works. You have to use two SimpleDateFormats, one for input and one for output, but it will give you just what you are wanting.

How to convert Java String "EEE MMM dd HH:mm:ss zzz yyyy"date type to Java util.Date "yyyy-MM-dd"

tl;dr
ZonedDateTime
.parse(
"Sat Dec 12 00:00:00 KST 2020" ,
DateTimeFormatter
.ofPattern( "EEE MMM dd HH:mm:ss zzz uuuu" )
.withLocale( Locale.US )
)
.toLocalDate()
.toString()

2020-12-12

Avoid legacy date-time classes

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes. Never use Date, Calendar, or SimpleDateFormat.

You said:

my target date type is Java Date type with format yyyy-MM-dd.

Apparently, you expect Date to hold a date. It does not. The java.util.Date class represents a moment, a date with time-of-day as seen in UTC. The java.sql.Date class pretends to hold only a date, but it too actually contains a date with time-of-day as seen in UTC.

Among the many problems with java.util.Date class is the behavior of its toString method. That method dynamically applies the JVM’s current default time zone while generating text. This anti-feature may contribute to your confusion.

LocalDate

Instead you should be using java.time.LocalDate to represent a date-only value without a time-of-day and without a time zone or offset-from-UTC.

ZonedDateTime

First use the DateTimeFormatter class to parse your entire input string. This results in a ZonedDateTime object representing a moment as seen through the wall-clock time used by the people of a particular region.

Example code
String input = "Sat Dec 12 00:00:00 KST 2020" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss zzz uuuu" ).withLocale( Locale.US ) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;

From that ZonedDateTime object, extract a LocalDate.

LocalDate localDate = zdt.toLocalDate() ;

See this code run live at IdeOne.com.

zdt.toString(): 2020-12-12T00:00+09:00[Asia/Seoul]

ld.toString(): 2020-12-12



About java.time

Table of all date-time types in Java, both modern and legacy

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. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

Why I can't parse this date format yyyy-MM-dd'T'HH:mm:ss.SSSZ?

You should use Z the same way you use T for the parser to recognize the character in format

val stringDate = "2021-12-16T16:42:00.000Z"
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
var consultationDate = sdf.parse(stringDate)

Java String to Date object of the format "yyyy-mm-dd HH:mm:ss"

java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse("2012-07-10 14:58:00.000000");

The mm is minutes you want MM

CODE

public class Test {

public static void main(String[] args) throws ParseException {
java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
.parse("2012-07-10 14:58:00.000000");
System.out.println(temp);
}
}

Prints:

Tue Jul 10 14:58:00 EDT 2012

iOS Swift 3 : Convert "yyyy-MM-dd'T'HH:mm:ssZ" format string to date object

You need an input format to convert the ISO8601 string to date and an output format to convert the date back to string:

let string = "2017-01-27T18:36:36Z"

let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")

Parsing Date and time from "yyyy-MM-dd hh:mm:ss.SSS" -------> "hh:mm aa" in android

Try this

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat output = new SimpleDateFormat("hh:mm aa");

Date d = null;
Date d1 = null;
try
{
d = input.parse("2018-02-05 17:08:52.503");
d1 = input.parse("2018-02-05 17:06:55.372");

} catch (ParseException e) {
e.printStackTrace();
}
String formatted = output.format(d);
String formatted1 = output.format(d1);

Log.i("DATE", "" + formatted);
Log.i("DATE1", "" + formatted1);

OUTPUT

I/DATE: 05:08 PM
I/DATE1: 05:06 PM

EDIT

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat output = new SimpleDateFormat("hh:mm aa");

Date d = null;
Date d1 = null;
try
{
d = input.parse("2018-02-05 17:00:52.503");
d1 = input.parse("2018-02-05 17:00:55.372");

} catch (ParseException e) {
e.printStackTrace();
}
String formatted = output.format(d);
String formatted1 = output.format(d1);

Log.i("DATE", "" + formatted);
Log.i("DATE1", "" + formatted1);

OUTPUT

I/DATE: 05:00 PM
I/DATE1: 05:00 PM


Related Topics



Leave a reply



Submit