SimpleDateFormat Japanese time [duplicate] - java

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.

Related

Java SimpleDateFormat can not parse date in "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX" format pattern [duplicate]

This question already has answers here:
String-Date conversion with nanoseconds
(5 answers)
StringDate to Date coming in different Time in SimpleDateFormat in java
(2 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 1 year ago.
I have simple code, which parses string into date.
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX").parse("2021-06-28T07:09:30.463931900Z")
It parses this string into Sat Jul 03 18:01:41 CEST 2021, which is not valid.
When I remove from pattern 'SSSSSSSSSX', it starts working and returns: Mon Jun 28 07:09:30 CEST 2021.
Problem is that I need nanoseconds, so I can not just get rid of this.
I found many similar topics, but any of them dealt with such date format.
Use java.time:
You can parse this example String without any explicit pattern, keep the precision as desired and, if necessary, format those date and time values in a multitude of custom ways.
Here's a small example:
public static void main(String[] args) {
// example String (of ISO format)
String input = "2021-06-28T07:09:30.463931900Z";
// parse it (using a standard format implicitly)
OffsetDateTime odt = OffsetDateTime.parse(input);
// print the result
System.out.println(odt);
// if you want a different output, define a formatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
// use a desired pattern
"EEE MMM dd HH:mm:ss O uuuu",
// and a desired locale (important for names)
Locale.ENGLISH);
// print that
System.out.println(odt.format(dtf));
}
This code example produces the following output:
2021-06-28T07:09:30.463931900Z
Mon Jun 28 07:09:30 GMT 2021

DateTimeFormatter throwing exception "could not be parsed at index 0" [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
DateTimeFormatter month pattern letter "L" fails
(3 answers)
Closed 2 years ago.
I keep on getting an error from running this code.
java.time.format.DateTimeParseException: Text 'Jan 03, 2020' could not be parsed at index 0
final String myFormat = "LLL dd, yyyy"; //sets format in which to show date (same as how its saved in database) ex. Jan 29, 2020
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(myFormat);
String startingBiWeeklyCheck = sharedPreferences.getString("biweekly start", "Jan 03, 2020");
LocalDate startingDate = LocalDate.parse(startingBiWeeklyCheck, dateFormatter);
Ive played around with the format but I'm not seeing why the pattern "LLL dd, yyyy" doesn't parse Jan 03, 2020
You should use MMM instead of LLL for month parsing.
Updated:
I was wrong about my answer above. It's the half of answer.
The deal is DateTimeFormatter.ofPattern(myFormat) uses default Locale.
For non-US locales, it doesn't work.
So you need to specify the locale according to your pattern.
DateTimeFormatter.ofPattern(myFormat).withLocale(Locale.US)

Java: format date string "150520" into "May 20, 2015" [duplicate]

This question already has answers here:
How to change the date format from YYMMDD to YYYY-MM-DD in java? [closed]
(2 answers)
Closed 5 years ago.
I'm using Java 8 on linux with the following code
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
LocalDate exampleDate = LocalDate.parse(myDate, formatter);
where myDate is a String equal to "150520". I'm getting error:
java.time.format.DateTimeParseException: Text '150520' could not
be parsed: Conflict found: Field MonthOfYear 1 differs from
MonthOfYear 5 derived from 2015-01-20
I'd like to return May 20, 2015 for example. Any idea what's wrong?
UPDATE
Replacing the date code D with d eliminates this error. How to format into readable date?
You want yyMMdd. The uppercase D parses "day of year" not "day of month".
[Edit] for the printing part, you could do DateTimeFormatter.ofPattern("MMM dd, yyyy").format(theDate).
Change
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
with
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd",Locale.ENGLISH);
You need to use this format string "yyMMdd". According to the docs
Symbol Meaning Presentation Examples
------ ------- ------------ -------
D day-of-year number 189
d day-of-month number 10

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.

Can't parse using SimpleDateFormat [duplicate]

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));

Categories