Joda-Time DateTime formatting based on locale

前端 未结 1 545
梦谈多话
梦谈多话 2021-01-04 11:41

I receive a string date from another system and I know the locale of that date (also available from the other system). I want to convert this String into a Joda-Time DateTim

相关标签:
1条回答
  • 2021-01-04 12:02
    String localizedCalendarDate = DateTimeFormat.shortDate().print(new LocalDate(2014, 9, 29));
    // uses in Germany: dd.MM.yyyy
    // but uses in US: MM/dd/yyyy
    
    LocalDate date =
      DateTimeFormat.mediumDate().withLocale(Locale.US).parseLocalDate("09/29/2014");
    DateTime dt = date.toDateTimeAtStartOfDay(DateTimeZone.forID("America/Los_Angeles"));
    

    As you can see, you will also need to know the clock time (= start of day in example) and time zone (US-California in example) in order to convert a parsed date to a global timestamp like DateTime.

    0 讨论(0)
提交回复
热议问题