首页 > 解决方案 > 获取第二天选定的小时纪元

问题描述

如果用户选择 01:25 AM,那么如何获取第二天该小时对应的纪元时间?

我做了类似的事情,这是非常“面部编码”:

private long returnEpochForThisHour(String hour){
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    Date date = new Date();
    String stringDate = dateFormat.format(date).substring(0, 10) + " " + hour;
    Date newDate = new Date(stringDate);
    Calendar c = Calendar.getInstance();
    c.setTime(newDate);
    c.add(Calendar.DATE, 1);
    Date lastDate = c.getTime();
    System.out.println(lastDate.getTime());
    return lastDate.getTime();
}

当做类似的事情时:

returnEpochForThisTime("01:25");

它返回:

1544491500000

但在 Android 设备中,它无法正常工作。有一个更好的方法吗?

标签: javaandroid

解决方案


推荐阅读