Skip to content

Instantly share code, notes, and snippets.

@ghusta
Created June 29, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghusta/7bff2e0aca0ef882c6e41a4a16ff9c42 to your computer and use it in GitHub Desktop.
Save ghusta/7bff2e0aca0ef882c6e41a4a16ff9c42 to your computer and use it in GitHub Desktop.
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