2016-10-15 230 views
0

我来自PHP背景,但学习Android,所以请原谅,如果这太基本了。转换时间:分钟上午/下午到unix时间戳

我有三个变量,其中包含小时,分钟和am/pm。我如何将它转换为unix时间戳,以便在几秒钟内获得组合值?

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? 
+1

使用java的日历类。您可以轻松地添加小时,分钟,秒或使用简单日期格式 –

+0

@HRaval仅供参考,'Calendar'类现在是遗留的,由java.time类代替。 –

回答

1

您应该使用LocalDateTime为:

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); 

如果你没有LocalDateTime的是,你可以使用的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); 

正如你看到的,代码几乎相同的。

如果你想保持当前的日期,切换到日历:

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())); 
+0

'LocalDateTime'出于某种原因在我的Android Studio版本中不可用 – asprin

+0

我为您添加了Java7版本。另请参阅@Basil Bourque的答案。 –

+0

但是,这将需要1970年1月1日的月份日期和年份。可以不设置为当前月份,年份和日期吗? – asprin

1

Answer by Soshin是好的。但我个人会分别处理日期和时间。

LocalDate

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuuMMdd"); 
LocalDate ld = LocalDate.parse("20160101" , dateFormatter); 

LocalTime

和时间的一天。不需要AM/PM部分;格式化代码指示预期的时间是在12小时时间还是24小时时间。

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

你可以将它们组合成一个LocalDateTime

LocalDateTime ldt = LocalDateTime.of(ld , lt); 

ZonedDateTime

如果你知道这个值的某些预期的时区,应用ZoneId得到ZonedDateTime

ZoneId z = ZoneId.of("America/Montreal"); 
ZonedDateTime zdt = ldt.atZone(z); 

A LocalDateTime没有意义;它不是一个没有从UTC或时区偏移的上下文的实际时刻。相比之下,ZonedDateTime确实是时间线上的实际点。

关于java.time

java.time框架是建立在Java 8和更高版本。这些类代替了日期 - 时间类legacy,如java.util.Date.Calendar,& java.text.SimpleDateFormat

Joda-Time项目现在位于maintenance mode,建议迁移到java.time。请参阅Oracle Tutorial。并搜索堆栈溢出了很多例子和解释。规格是JSR 310

从何处获取java.time类?

ThreeTen-Extra项目与其他类扩展java.time。这个项目是未来可能增加java.time的一个试验场。您可以在这里找到一些有用的类,如Interval,YearWeek,YearQuartermore

+0

由于某种原因,LocalDate在我的Android Studio版本中不可用 – asprin

+0

@asprin在我的答案中重新阅读子弹“Android”。 –

相关问题