Reputation: 553
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 I get is
2015年07月19日(日)
Now I want to convert this date back to Western date as this format "yyyy-MM-dd" but I can't figure how to do that. Please help me! Thank you!
Upvotes: 4
Views: 2181
Reputation: 153
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());
}
Upvotes: 0
Reputation: 473
The formate you given is the locale Japanese format, so that you can use the default option.
For your convenience please refer the java documentation here http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
Try this one
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, new Locale("ja"));
System.out.println(df.parse("2015年07月20日"));
System.out.println(df.format(new Date()));
output should be like this:
Mon Jul 20 00:00:00 IST 2015
2015年07月20日
Refer previous ideone answer here IDEONE
Upvotes: 1
Reputation: 12453
Don't convert an Object into a String, work with it and parse it back. Keep the original informtion in the Date object and render the output (read only) where ever you need it:
// 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));
Prints out:
2015年7月20日 (月曜日)
Montag, 20. Juli 2015
Upvotes: 2