2

I have time in milliseconds I need to convert it to RFC-822 format. Is there generic Java library that can I use? What is the best practice of doing it?

Example time in milliseconds: 1440612000000 Time in RFC-822: Wed, 02 Oct 2002 08:00:00 EST

Thanks in advance.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Renat Gatin
  • 6,053
  • 5
  • 37
  • 58

2 Answers2

4

Use the SimpleDateFormat

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
dateFormat.setTimeZone(TimeZone.getTimeZone("EST")); //To use the EST time zone as in your example
dateFormat.format(new Date(timeInMiliseconds));

As an example, with 1440612000000L outputs Wed, 26 Aug 2015 13:00:00 EST.

eric.m
  • 1,577
  • 10
  • 20
  • 1
    Thanks, but I can't hard code the TimeZone. I have an Android app, and what if my clients are from different TimeZones? – Renat Gatin Aug 26 '15 at 18:56
  • 1
    That was to match your example, but you can remove that line if you want. Then, it will use the system's timezone. If it doesn't work, try to use `TimeZone.getDefault()` instead. – eric.m Aug 26 '15 at 19:04
  • Thanks! You have been most helpful! – Renat Gatin Aug 26 '15 at 19:10
  • Can you, please, also tell me what is default RFC-822 format? Because I can see, someone is using "EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", another one is "EEE, dd MMM yyyy HH:mm:ss zzz".. Which one format for example can be parsed by apache http-commons? – Renat Gatin Aug 26 '15 at 19:14
  • 1
    I didn't know of RFC-822 existance until you asked this question. I just formatted as you did in your example, as I don't know anything about that RFC-822 format. – eric.m Aug 26 '15 at 19:18
1

You can use the calendar class included in Java:

Calendar calendar = Calendar.getInstance();
// set time in millis
calender.setTimeInMillis(millis);

int year       = calendar.get(Calendar.YEAR);
int month      = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); 
int dayOfWeek  = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

int hour       = calendar.get(Calendar.HOUR);        // 12 hour clock
int hourOfDay  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute     = calendar.get(Calendar.MINUTE);
int second     = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);

System.out.println(sdf.format(calendar.getTime()));

System.out.println("year \t\t: " + year);
System.out.println("month \t\t: " + month);
System.out.println("dayOfMonth \t: " + dayOfMonth);
System.out.println("dayOfWeek \t: " + dayOfWeek);
System.out.println("weekOfYear \t: " + weekOfYear);
System.out.println("weekOfMonth \t: " + weekOfMonth);

System.out.println("hour \t\t: " + hour);
System.out.println("hourOfDay \t: " + hourOfDay);
System.out.println("minute \t\t: " + minute);
System.out.println("second \t\t: " + second);
System.out.println("millisecond \t: " + millisecond);

Then you can just build a string out of the values. E.g. :

String s = calender.getDisplayName(Calender.DAY_OF_WEEK, Calender.SHORT, locale) + ", " + calender.get(Calender.DAY_OF_MONTH) + " " + calender.getDisplayName(Calender.MONTH, Calender.SHORT, locale) + " " + calender.get(Calender.YEAR);

will print: Wed, 02 Oct 2002

TameHog
  • 950
  • 11
  • 23
  • 2
    In addition to this answer, also see [Joda-Time](http://www.joda.org/joda-time/) as a (imho, better) substitute for Java's built-in `Calendar` class. – Matthew Herbst Aug 26 '15 at 18:39