Sorry if my question has already been answered, but I can't really find what I am looking for. So I have this time "1228061700000" and I want to convert it to show only the date, like this 17/6/08. It would also be good to check for the locale, so for US locale the format to be like this 6/17/08 and for european locale to be 17/6/08... How can I do it?
Asked
Active
Viewed 81 times
0
-
2some code would be helpful... – Sorin Adrian Carbunaru Dec 20 '13 at 10:46
-
You need to convert from "1228061700000" or it doesn't matter? – Arnas Ivanavičius Dec 20 '13 at 10:48
-
I need to convert from many such dates, so I just need a good example to work with – Sartheris Stormhammer Dec 20 '13 at 10:59
3 Answers
4
Use a DateFormat:
DateFormat f = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
String result = f.format(new Date(millis));

Cedric Reichenbach
- 8,970
- 6
- 54
- 89
2
long milliSeconds=1228061700000l;
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //change your date time formate as required
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));

Satheesh Cheveri
- 3,621
- 3
- 27
- 46
-
2Calendar is expensive, I wouldn't set a Calendar just to get a Date from it. Try `new Date(millisSeconds)` instead. – Peter Lawrey Dec 20 '13 at 10:50
2
DateFormat.getDateInstance(int style, Locale aLocale)This displays the current date in a locale-specific way.
So, you can try:
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, yourLocale);
String formattedDate = df.format(yourDate);
In this case you can choose what type of yourDate
is.
Your locate can be changed to Locale.UK
Locale.US
and etc.

Arnas Ivanavičius
- 959
- 7
- 22