Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Joda Time DateTime formatted according to Locale
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
/**
* https://stackoverflow.com/questions/26102295/joda-time-datetime-formatting-based-on-locale
*/
public class JodaTimeLocaleTest
{
private LocalDateTime localDateTime;
private DateTime dateTimeNow = DateTime.now().withTimeAtStartOfDay();
@Before
public void setUp() throws Exception
{
localDateTime = new LocalDateTime(2016, 12, 31, 23, 59, 59);
}
@Test
public void testLocaleDefault() throws Exception
{
Locale locale = Locale.getDefault();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.shortDateTime().withLocale(locale);
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(localDateTime));
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(dateTimeNow));
}
@Test
public void testLocale_FR() throws Exception
{
Locale locale = Locale.FRANCE;
DateTimeFormatter dateTimeFormatter = DateTimeFormat.shortDateTime().withLocale(locale);
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(localDateTime));
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(dateTimeNow));
}
@Test
public void testLocale_DE() throws Exception
{
Locale locale = Locale.GERMANY;
DateTimeFormatter dateTimeFormatter = DateTimeFormat.shortDateTime().withLocale(locale);
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(localDateTime));
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(dateTimeNow));
}
@Test
public void testLocale_CN() throws Exception
{
Locale locale = Locale.CHINA;
DateTimeFormatter dateTimeFormatter = DateTimeFormat.shortDateTime().withLocale(locale);
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(localDateTime));
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(dateTimeNow));
}
@Test
public void testLocale_JP() throws Exception
{
Locale locale = Locale.JAPAN;
DateTimeFormatter dateTimeFormatter = DateTimeFormat.shortDateTime().withLocale(locale);
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(localDateTime));
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(dateTimeNow));
}
@Test
public void testLocale_US() throws Exception
{
Locale locale = Locale.US;
DateTimeFormatter dateTimeFormatter = DateTimeFormat.shortDateTime().withLocale(locale);
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(localDateTime));
System.out.println(locale.toString() + " <> " + dateTimeFormatter.print(dateTimeNow));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment