如何将特定时间转换为毫秒

How to convert a specific time to milliseconds

如何转换一个特定的小时,例如18:00,到毫秒?

我需要这个,这样如果它介于 12:00 和 18:00 之间,我可以做一些事情,如果它不在 12:00 和 18:00 之间,我可以做点别的。

我得到当前时间:

Private long time;
time = System.currentTimeMillis();

但我不知道如何将小时转换为毫秒。

我在 Internet 上搜索过,似乎如果我使用 System.currentTimeMillis(),它会给我当前日期和时间的毫秒数,但我需要它每天更新如下:

今天的毫秒时间是这样的:1516994140294。但是这个数字包含这个:Fri Jan 26 2018 19:15:40。因此,如果我将毫秒用于今天的 12:00 和 18:00,这意味着明天它将无法正常工作。

那么你能帮我举个例子或文档吗?一切都可以帮助我 :) 并提前致谢。

  1. 用“:”分隔符分隔当前时间。第一个是小时,第二个是分钟,第三个是秒。
  2. 将小时数转换为分钟数。即18*60.
  3. 在本例中将上述答案添加到当前分钟 (18*60 + 00)。
  4. 将上面的答案乘以60秒((18*60 + 00)*60)。
  5. 同样,采用上述方法,并在本例中添加秒数 ((18*60 + 00)*60 + 00)。
  6. 同样,采用上面的方法,乘以 1000 毫秒 (((18*60 + 00)*60 + 00)*1000).

中提琴,你的时间以毫秒为单位。

你应该看看 SimpleDateFormat

有一个很好的教程here

要比较两个 "dates",您应该查看如何创建 Calendar(或更简化但已弃用的 Date)对象。它实现了可比性,因此您可以将当前时间转换为 Date 对象并将其与已知时间戳进行比较(如您的问题):

Java Calendar JavaDate

您可以将 CalendarDate 都设置为特定时间或当前时间。例如 Calendar.setCurrentTimeMillis(System.currentTimeMillis()) 将获得一个 Calendar 实例设置当前时间(就像 Calendar.getInstance())。

这里有一个 Date 的例子:

    // This is how to get today's date in Java
    Date today = new Date();

    //If you print Date, you will get un formatted output
    System.out.println("Today is : " + today);

    //formatting date in Java using SimpleDateFormat
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
    String date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yyyy format : " + date);

    //Another Example of formatting Date in Java using SimpleDateFormat
    DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd/MM/yy pattern : " + date);

    //formatting Date with time information
    DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

    //SimpleDateFormat example - Date with timezone information
    DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z");
    date = DATE_FORMAT.format(today);
    System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date);

来自此站点:http://www.java67.com/2013/01/how-to-format-date-in-java-simpledateformat-example.html#ixzz55Jw9BaXW

tl;博士

LocalTime.now()                               // Capture current moment. Better to explicitly pass a `ZoneId` object than rely implicitly on the JVM’s current default time zone.
         .isBefore( LocalTime.of( 18 , 0 ) )  // Compare the current time-of-day against the limit.

不需要毫秒计数

不需要计算毫秒。 Java 为这项工作提供了智能日期时间 类,可在 Java 8 及更高版本内置的 java.time 包中找到。对于较早的 Android,请参阅下面的最后一个项目符号。

不需要System.currentTimeMillis()。 java.time 类 做同样的工作。

LocalTime

要获取当前时间,不带日期和时区,请使用 LocalTime

确定当前时间需要时区。对于任何给定时刻,一天中的时间因区域而异。如果省略,则隐式应用 JVM 当前的默认时区。最好明确指定您的 desired/expected 时区,因为默认值可能会在运行前或运行期间随时更改。

指定 proper time zone name in the format of continent/region, such as America/Montreal, Africa/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们 不是 真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalTime now = LocalTime.now( z ) ;

与方法 isBeforeisAfterequals 进行比较。

询问 "is equal to or later than noon" 的更短方式是 "is not before noon"。

boolean isAfternoon = 
    ( ! now.isBefore( LocalTime.NOON ) ) 
    && 
    now.isBefore( LocalTime.of( 18 , 0 ) )
;

如果您担心 Daylight Saving Time( DST) 等异常的影响,请探索使用 ZonedDateTime 而不仅仅是 LocalTime


关于java.time

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.

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

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

在哪里获取java.time类?

  • Java SE 8, Java SE 9,及以后
    • 内置。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and Java SE 7
  • Android
    • Android java.time 类.
    • 捆绑实施的更高版本
    • 对于较早的 Android,ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See