Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Time in milliseconds to HTTP format?

Tags:

java

date

http

I have a File object and I want to get the last modified date of that file into HTTP format. The format is in GMT time and is like:

Mon, 10 Feb 2014 16:17:37 GMT

I know java.io.File has a method lastModified() which returns the time in milliseconds. I can also pass that time in milliseconds to the constructor of the java.util.Date class. But what is the easiest way to get a string in HTTP format?

Thanks.

like image 842
Rickkwa Avatar asked May 24 '26 13:05

Rickkwa


1 Answers

SimpleDateFormat sdf = 
  new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String httpDate = sdf.format(new Date(file.lastModified()));
like image 158
Meno Hochschild Avatar answered May 26 '26 11:05

Meno Hochschild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!