Can't parse using SimpleDateFormat [duplicate] - java

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 7 years ago.
I'm trying to parse "Dec 6 04:13:01" with "MMM d HH:mm:ss", but it is not working! I spent a lot of time but cant figure it out.
Any ideas why it fails?

You are probably trying to parse it with JAPANESE locale (guessing it from your profile + your web page), specify any english locale for example: Locale.US
String dateString = "Dec 6 04:13:01";
DateFormat df = new SimpleDateFormat("MMM d HH:mm:ss", Locale.US);
System.out.println(df.parse(dateString));

Related

SimpleDateFormat Japanese time [duplicate]

This question already has answers here:
Java Date() giving the wrong date [duplicate]
(9 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Invalid date is populated when we use yyyy-MM-dd'T'HH:mm:ssXXX format in java [duplicate]
(1 answer)
Closed 2 years ago.
The time I have is -> September 15 2020 11:10:25
I am using this code to format it in Japanese
timeFormatStr = "YYYY MMMMMMMMMM DD HH:mm:ss z";
SimpleDateFormat sd = new SimpleDateFormat(timeFormatStr, locale);
timeStr = sdf.format(new Date(time));
The timeStr looks like this (does not look right).
2020 9月 259 23:10:25 UTC
Any idea what the format string should be? I checked that the locale is - ja_JP.eucjp
Thanks
YYYY MMMMMMMMMM DD HH:mm:ss z is not how the Japanese format their dates and times. You should use DateTimeFormatter, and call ofLocalizedDateTime and withLocale. This will produce a formatter that produces strings in a native Japanese format.
String formatted = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL) // choose a style here
.withLocale(Locale.JAPANESE)
.format(new Date(time).toInstant().atZone(ZoneOffset.UTC)); // choose a timezone here
System.out.println(formatted); // 1970年1月1日木曜日 0時00分00秒 Z
You shouldn't really be using Dates anymore. You should instead give the DateTimeFormatter a ZonedDateTime directly.

Parsing timestamp with '+' into Date or Instant in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 2 years ago.
I have timestamp of the format 2020-05-12 12:00:00+00 . How can I parse into Java.util.Date and Java.time.Instant.
Seemingly basic question, but I suspect it is the source of the problem that I am yet to solve in thread
Java 8 introduced DateTimeFormatter. Here is the link to the DateTimeFormatter Documentation.
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssx");
String tsFromDb = "2020-05-12 12:00:00+00";
Instant inst = formatter.parse(tsFromDb, Instant::from);
System.out.println(inst);
Output:
2020-05-12T12:00:00Z
If you need a java.util.Date:
Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);
Output in America/Tijuana time zone:
Tue May 12 05:00:00 PDT 2020

Change Time zone and date format [duplicate]

This question already has answers here:
How to set the TimeZone for String parsing in Android
(3 answers)
Closed 6 years ago.
I get from the server is like 2017-01-24T16:16:30.690Z.
This date is in GMT time zone.
I want to convert this time into GMT+6 time zone as well as time format.
My expected result is: 24 January 2017 22:16
See above comments. If you apply those, try:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("2017-01-24T16:16:30.690Z", new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+06:00"));
sdf.applyPattern("d MMMM yyyy HH:mm");
String formatted = sdf.format(date);
Worked for me.

Display date time in readable format [duplicate]

This question already has answers here:
How do I change date time format in Android?
(11 answers)
Closed 6 years ago.
I receive DATETIME from mysql database in a format like this 2016-10-12 18:52:00. Is there an easy way to print it in a readable like 12 October 2016 6:52 pm in java or android?
You can do things like:
SimpleDateFormat simpleDate = new SimpleDateFormat("MM/DD/YYYY HH:mm:ss");
Date date = new Date();
String S = simpleDate.format(date); //This will give you like 11/20/2016 08:45:02
Now use this form and get the month name programatically according to your need.

Creating date in java with SimpleDateFormat [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
Update:
Really?!!! Duplicate??? My format is correct (yyyy/MM/dd HH:mm:ss) but return time is incorrect. How this is similar to another question????
I'm trying to create java Date but it's always return wrong value. This is my code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS");
Date GreDate = dateFormat.parse("2014/03/22 00:00:00");
And GreDate return Sun Dec 22 00:00:00 GMT+03:30 2013 as value.
Please don't suggest to use external library for date type.
Update:
I changed my pattern to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Now GreDate returns Sat Mar 22 01:00:00 GMT+04:30 2014. Year is correct but time still not 00:00:00.
Note that:
MM are the months, mm are the minutes.
SS are the milliseconds, ss are the seconds.
So you need to change your dateFormat to
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Basically there are two errors in your pattern, both in the time part (seconds and minutes).
Here is the link to the complete documentation link.

Categories