Reputation: 9823
I'm from PHP background but learning Android, so please excuse me if this is too basic.
I have three variables which contain hours, minutes, and am/pm. How would I convert it to a unix timestamp so that I get the combined value in seconds?
String hours = String.valueOf(hourBox.getText()); // may contain a value like 05
String minutes = String.valueOf(minuteBox.getText()); // may contain a value like 45
String ampm = String.valueOf(ampmBox.getText()); // may contain a value like PM or AM
// PHP way
$timestamp = date('Y-m-d '.$hours.':'.$minutes.' '.$ampm);
echo strtotime($timestamp);
// What would be the equivalent of this in Java?
Upvotes: 0
Views: 2881
Reputation: 17711
You should use LocalDateTime for that:
String hours = "05";
String minutes = "45";
String ampm = "PM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd hh:mm a");
LocalDateTime dateTime = LocalDateTime.parse(String.format("20160101 %s:%s %s", hours, minutes, ampm), formatter);
System.out.println(dateTime);
In case you don't have LocalDateTime yet, you can use SimpleDateFormat:
String hours = "05";
String minutes = "45";
String ampm = "PM";
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date dateTime = format.parse(String.format("%s:%s %s", hours, minutes, ampm));
System.out.println(dateTime);
As you see, the code is pretty much the same.
If you want to keep the current date, switch to Calendar:
String hours = "05";
String minutes = "45";
String ampm = "PM";
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR, Integer.parseInt(hours));
calendar.set(Calendar.MINUTE, Integer.parseInt(minutes));
calendar.set(Calendar.AM_PM, "AM".equals(ampm) ? 0 : 1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
System.out.println(format.format(calendar.getTime()));
Upvotes: 1
Reputation: 338654
The Answer by Soshin is good. But personally I would work with the date and the time separately.
LocalDate
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern( "uuuuMMdd" );
LocalDate ld = LocalDate.parse( "20160101" , dateFormatter );
LocalTime
And the time-of-day. No need for the AM/PM part; the formatting code indicates whether the expected time-of-day is in 12-hour time or 24-hour time.
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern( "hhmm" ); // hh = two digits of 12-hour time (1-12). mm = minute-of-hour.
LocalDate ld = LocalDate.parse( hours + minutes , timeFormatter ); // 0545 in afternoon.
LocalDateTime
You can combine them into a LocalDateTime
.
LocalDateTime ldt = LocalDateTime.of( ld , lt );
ZonedDateTime
If you know for certain the intended time zone of this value, apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( z );
A LocalDateTime
has no meaning; it is not an actual moment without the context of an offset-from-UTC or a time zone. A ZonedDateTime
in contrast is indeed an actual point on the timeline.
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
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
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?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 1