How can i convert japanese date to western date in java?

I have methods like this:

//datetime is millisecond
public static String getStringDateFormatMonth(long datetime) {
          String yearlessPattern = "yyyy年MM月";
          SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern);
          Date date = new Date(datetime);
          String datestr = yearlessSDF.format(date);
          return datestr;

         }

public static String getStringDateFormat(long datetime) {
          String yearlessPattern = "dd日";
          SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern);
          SimpleDateFormat sdfDay = new SimpleDateFormat("E", Locale.JAPAN);
          Date date = new Date(datetime);
          String datestr = yearlessSDF.format(date) + "(" + sdfDay.format(date) + ")";
          return datestr;

         }

Intialize string a:

String a = LifelogUtil.getStringDateFormatMonth(currentDate.getTimeInMillis())
                    + LifelogUtil.getStringDateFormat(currentDate.getTimeInMillis());

and the result is

2015年07月19日(日)

Now I want to convert this date to a western date like this format "yyyy-MM-dd", but I cannot figure out how to do this. Please help me! Thank!

+4
source share
3 answers

The format you specified is the Japanese locale format, so you can use the default option. For your convenience, please refer to the java documentation here http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
Try this

DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, new Locale("ja"));
System.out.println(df.parse("2015年07月20日"));
System.out.println(df.format(new Date()));

The output should look like this:

Mon Jul 20 00:00:00 IST 2015
2015年07月20日

Pay attention to the previous ideal IDEONE answer

+2

, . Date ( ), :

    // use an object internally:
    Date anyDate = new Date();

    // can also be SimpleDateFormat, etc:
    DateFormat japaneseDf = DateFormat.getDateInstance(DateFormat.FULL, Locale.JAPAN);
    DateFormat germanDf = DateFormat.getDateInstance(DateFormat.FULL, Locale.GERMANY);

    // when you need to display it somewhere render it appropriately, not changing the data:
    System.out.println(japaneseDf.format(anyDate));
    System.out.println(germanDf.format(anyDate));

:

2015年7月20日 (月曜日)
Montag, 20. Juli 2015
+2

This should help.

        SimpleDateFormat jp= new SimpleDateFormat("yyyy年MM月dd日(E)",Locale.JAPAN); //Japan Format
        SimpleDateFormat west = new SimpleDateFormat("yyyy-MM-dd"); //Western Format
        try{
            Date date = jp.parse(a); //convert the String to date
            System.out.println(west.format(date));//format the date to Western 
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
0
source

Source: https://habr.com/ru/post/1598657/


All Articles