0

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?

Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81

3 Answers3

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
  • 2
    Calendar 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.