0

I have a time picker in android. When the user chooses the time, it is returned two integers (hourOfDay and minute) through onTimeSet method. Its signature is shown bellow:

public void onTimeSet(TimePicker view, int hourOfDay, int minute);

What I want is to show to the user the chosen time in the local pattern. So, in Brazil or France, the time should be seen as 14:30, while in the US, 2:30 PM. At the moment I'm using joda-time library and not LocalTime, because android does not support the last one. If I can get the String pattern of time the problem is solved, because I can call the toString(String pattern) method.

LocalTime localTime = new LocalTime(hourOfDay, minute);
localTime.toString(pattern /* to be found */)

I have the same problem with another Picker. The user chooses the date and year, month and dayOfMonth are returned. In this case, in Brazil or France the date should be display as 12/01/2000 while in the US 1/12/2000.

How can I solve this? Any ideas?

Thanks in advance

Bernardo Peters
  • 163
  • 2
  • 9
  • Tip: Use the term *[localized](https://en.wikipedia.org/wiki/Internationalization_and_localization)* when referring to strings formatted using a particular human language for translation and certain cultural norms for abbreviation/punctuation/ordering. The term “local” in date-time work often means “without any offset-from-UTC, without any time zone”, an altogether different matter. In java.time, the classes named with `Local…` (`LocalDate`, `LocalTime`, and `LocalDateTime`) all mean no-offset-nor-zone — their naming has nothing to do with localization. – Basil Bourque May 12 '17 at 06:15

1 Answers1

2

The Joda-Time library is now in maintenance mode, with the team advising migration to the java.time classes.

Use the java.time framework of the ThreeTenABP library.

LocalDate ld = LocalDate.of( 2017 , 1 , 23 );
LocalTime lt = LocalTime.of( 12 , 23 ) ;
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z );

Locale l = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG ).withLocale( l );
String output = zdt.format( f );

Adjust to suit your taste by calling ofLocalizedDate or ofLocalizedTime, using other FormatStyle objects, other Locale objects, other zones.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154