Rich Oliver Rich Oliver - 1 year ago 115
Scala Question

Scala: How do I get the current time in RFC 822 / 1123 format

How do I get the current time GMT date-time in RFC 822 / RFC 1123 format in Scala, to put into an HTTP header? Preferably using the built in Java 8 classes.

For some reason what I thought would be a simple generic task has eluded me.

Answer Source

You can use ZonedDateTime to get the current date time and DateTimeFormatter to format it using its existing formatter for RFC 822 / 1123: DateTimeFormatter.RFC_1123_DATE_TIME

import java.time.{ZonedDateTime, ZoneOffset}
import java.time.format.DateTimeFormatter

val datetime =
val formatter = DateTimeFormatter.RFC_1123_DATE_TIME

// String = Wed, 16 Sep 2015 21:45:10 GMT
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download