Last active
February 19, 2020 16:09
-
-
Save tarot/2269a0abf9ffa7b8487a to your computer and use it in GitHub Desktop.
Apexでhttp-date (RFC1123)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateFormat { | |
private static final Map<String, Integer> MONTHS = new Map<String, Integer> { | |
'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, | |
'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12 | |
}; | |
public static Datetime fromRFC1123(String x) { | |
String[] dateParts = x.split('[\\s:]'); | |
Integer year = Integer.valueOf(dateParts[3]); | |
Integer month = MONTHS.get(dateParts[2]); | |
Integer day = Integer.valueOf(dateParts[1]); | |
Integer hour = Integer.valueOf(dateParts[4]); | |
Integer minute = Integer.valueOf(dateParts[5]); | |
Integer second = Integer.valueOf(dateParts[6]); | |
return Datetime.newInstanceGmt(year, month, day, hour, minute, second); | |
} | |
public static String toRFC1123(Datetime x) { | |
return x.formatGmt('EEE, dd MMM yyyy HH:mm:ss z'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment