Created
March 1, 2017 01:52
-
-
Save xak2000/adfd9b55e040a0bf33ba43df94a78d3f to your computer and use it in GitHub Desktop.
Format absolute and relative dates with specified timezone offset
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
@Grapes( | |
@Grab(group='joda-time', module='joda-time', version='2.3') | |
) | |
import java.text.SimpleDateFormat; | |
import org.joda.time.DateTime; | |
import org.joda.time.DateTimeZone; | |
import org.joda.time.Duration; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.format.PeriodFormatterBuilder; | |
import java.time.ZoneOffset; | |
import java.time.format.DateTimeFormatter; | |
// | |
// Different ways of date formatting with offset timezone (GMT+N) | |
// | |
def locale = new Locale("ru"); | |
//////////// | |
// Java 7 // | |
//////////// | |
// format absolute time | |
def j7Pattern = "dd MMMM 'in' HH:mm"; | |
def j7TimeZone = TimeZone.getTimeZone("GMT+3"); | |
def j7Formatter = new SimpleDateFormat(j7Pattern, locale); | |
j7Formatter.setTimeZone(j7TimeZone); | |
println("Java 7: " + j7Formatter.format(new Date())); | |
// format relative time | |
// 'через' mm 'мин' не работает, т.к. mm - это кол-во минут с текущего часа, а вовсе не разница между двумя датами в минутах | |
// j7Pattern = "'after' mm 'min'"; | |
// j7Formatter = new SimpleDateFormat(j7Pattern, locale); | |
// j7Formatter.setTimeZone(j7TimeZone); | |
// println(j7Formatter.format(new Date())); | |
def j7Minutes = Math.round(((new Date().getTime() + 120000) / 60000) - ((new Date().getTime()) / 60000)); | |
println("Java 7: " + "after " + j7Minutes + " min"); | |
/////////////// | |
// Joda Time // | |
/////////////// | |
// format absolute time | |
def jtPattern = "dd MMMM 'in' HH:mm"; | |
def jtTimeZone = DateTimeZone.forOffsetHours(3); | |
def jtFormatter = DateTimeFormat.forPattern(jtPattern).withZone(jtTimeZone).withLocale(locale); | |
println("Joda : " + jtFormatter.print(new DateTime(new Date()))); | |
// format relative time | |
def jtDuration = new Duration(new DateTime(new Date()), new DateTime(new Date()).plusMinutes(2)); | |
def jtPeriodFormatter = new PeriodFormatterBuilder() | |
.appendPrefix("after ") | |
.appendMinutes() | |
.appendSuffix(" min") | |
.toFormatter(); | |
println("Joda : " + jtPeriodFormatter.print(jtDuration.toPeriod())); | |
//////////// | |
// Java 8 // | |
//////////// | |
// format absolute time | |
def j8Pattern = "dd MMMM 'in' HH:mm"; | |
def j8ZoneOffset = ZoneOffset.ofHours(3); | |
def j8Formatter = DateTimeFormatter.ofPattern(j8Pattern, locale); | |
println("Java 8: " + new Date().toInstant().atOffset(j8ZoneOffset).format(j8Formatter)); | |
// format relative time | |
def j8Duration = java.time.Duration.between(new Date().toInstant(), new Date().toInstant().plusSeconds(120)); | |
println("Java 8: " + "after " + j8Duration.toMinutes() + " min"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment