Amit Unakal
Amit Unakal

Reputation: 31

How to Format English Date in Japanese with ERA

I want the new Japanese ERA Date as "R010501", whereas I am getting "R151".

I am using the com.ibm.icu.text.DateFormat package to get the date format


Date dtEngDate = new SimpleDateFormat("yyyy-MM-dd").parse("2019-05-01");

com.ibm.icu.util.Calendar japaneseCalendar = new com.ibm.icu.util.JapaneseCalendar();

        com.ibm.icu.text.DateFormat japaneseDateFormat = japaneseCalendar.getDateTimeFormat(
                 com.ibm.icu.text.DateFormat.SHORT, -1, Locale.JAPAN);      

        String today = japaneseDateFormat.format(dtEngDate);
System.out.println("today is:" +today.replaceAll("/", ""));

Output: today is --> R151. Expected Output: today is --> R010501

Upvotes: 3

Views: 2392

Answers (2)

Anonymous
Anonymous

Reputation: 86296

java.time.chrono.JapaneseDate

You don’t need an external dependency for the Japanese calendar. It’s built-in, in the JapaneseDate class.

Beware: Only the most recent versions of Java know about the new Reiwa era.

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("GGGGGyyMMdd", Locale.JAPANESE);
    LocalDate isoDate = LocalDate.parse("2019-05-01");
    JapaneseDate japaneseDate = JapaneseDate.from(isoDate);
    System.out.println(japaneseDate.format(dateFormatter));

Output from this snippet on Java 11.0.3 is:

R010501

Five pattern letters G gives the narrow form of the era, just one letter (here R for Reiwa).

On Java 9.0.4 I got H310501. So it seems that it will work correctly if you are able to upgrade your Java. Meno Hochschild reports in a comment that he got the correct result on Java 8u212, so you may not need to upgrade to a new major version if only you’ve got the newest minor upgrade within your major version. I don’t know if there’s a way to upgrade only the calendar data in an older Java version, it might be another thing to investigate.

BTW don’t use SimpleDateFormat and Date. Those classes are poorly designed (the former in particular notoriously troublesome) and long outdated. Use java.time, the modern Java date and time API. It’s so much nicer to work with.

Link: Oracle Tutorial: Date Time explaining the use if java.time.

Upvotes: 2

Nicola Uetz
Nicola Uetz

Reputation: 858

I don't know what exactly you did other than me but I just downloaded the com.ibm.icu library from http://www.java2s.com/Code/Jar/c/Downloadcomibmicu442jar.htm and basically copied your code.

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.JapaneseCalendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {

    public static void main(String[] args) throws ParseException {
        Date dtEngDate = new SimpleDateFormat("yyyy-MM-dd").parse("2019-05-01");
        Calendar japaneseCalendar = new JapaneseCalendar();
        DateFormat japaneseDateFormat = japaneseCalendar.getDateTimeFormat(DateFormat.SHORT, -1, Locale.JAPAN);
        String today = japaneseDateFormat.format(dtEngDate);
        System.out.println("today is: " + today.replaceAll("/", ""));
    }

}

I'm getting today is: 平成310501 as console output and I guess this is what you are looking for. So I guess there is something wrong with your com.ibm.icu-4.4.2.jar.

Maybe consider retrying to download the latest version from the link I used and adding it to the modules/projects dependencies.

Upvotes: 1

Related Questions