Time consts in Java?

前端 未结 13 602
遇见更好的自我
遇见更好的自我 2020-12-23 15:45

Is there a Java package with all the annoying time constants like milliseconds/seconds/minutes in a minute/hour/day/year? I\'d hate to duplicate something like that.

相关标签:
13条回答
  • 2020-12-23 16:31

    Joda Time also has a DateTimeConstants class with things like MILLIS_PER_SECOND, SECONDS_PER_MINUTE, MILLIS_PER_DAY, etc, etc.

    0 讨论(0)
  • 2020-12-23 16:31

    If you mean to obtain the values Calendar have all fields related to time management, with some simple reflection you can do

    Field[] fields = Calendar.class.getFields();

    for (Field f : fields)
    {
      String fName = f.toString();
      System.out.println(fName.substring(fName.lastIndexOf('.')+1).replace("_", " ").toLowerCase());
    }
    

    this will output:

    era
    year
    month
    week of year
    week of month
    date
    day of month
    day of year
    day of week
    day of week in month
    am pm
    hour
    hour of day
    minute
    second
    millisecond
    zone offset
    dst offset
    field count
    sunday
    monday
    tuesday
    wednesday
    thursday
    friday
    saturday
    january
    february
    march
    april
    may
    june
    july
    august
    september
    october
    november
    december
    undecimber
    am
    pm
    all styles
    short
    long
    

    from which you can exclude what you don't need.

    If you need just constants you have them: Calendar.DAY_OF_MONTH, Calendar.YEAR and so on..

    0 讨论(0)
  • 2020-12-23 16:32

    The Java TimeUnit seems to be what you want

    0 讨论(0)
  • 2020-12-23 16:37

    Java 8 / java.time solution

    As an alternative to TimeUnit, you might for some reason prefer the Duration class from java.time package:

    Duration.ofDays(1).getSeconds()     // returns 86400;
    Duration.ofMinutes(1).getSeconds(); // 60
    Duration.ofHours(1).toMinutes();    // also 60
    //etc.
    

    Additionally, if you would go deeper and have analyzed how Duration.ofDays(..) method works, you would see the following code:

    return create(Math.multiplyExact(days, SECONDS_PER_DAY), 0);
    

    where SECONDS_PER_DAY is a package protected static constant from LocalTime class.

    /**
     * Seconds per day.
     */
    static final int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
    
    //there are also many others, like HOURS_PER_DAY, MINUTES_PER_HOUR, etc.
    

    I think it is safe to assume that if there would be any package, which would defined "all the annoying time constants like miliseconds/seconds/minutes" as you call them, I believe Java SDK Developers would have use them.

    Why are this LocalTime constants package protected and not public that is a good question, I believe there is a reason for that. For now it looks like you really have to copy them and maintain on your own.

    0 讨论(0)
  • 2020-12-23 16:37

    One more approach with already baked (for multiple call) Duration instances (and 0 math operations):

    ChronoUnit.DAYS.getDuration().getSeconds()
    
    0 讨论(0)
  • 2020-12-23 16:39

    While TimeUnit discussed in this Answer and Duration discussed in this Answer probably more directly addresses the Question, there are some other handy units-of-time features in Java.

    java.time

    For units, see the ChronoUnit enum:

    • FOREVER
    • ERAS
    • MILLENNIA
    • CENTURIES
    • DECADES
    • YEARS
    • MONTHS
    • WEEKS
    • DAYS
    • HALF_DAYS
    • HOURS
    • MINUTES
    • SECONDS
    • MILLIS
    • MICROS
    • NANOS

    The java.time package has sophisticated enums for DayOfWeek and Month. So rather than pass around a mere number or string, you can pass full-fledged objects such as DayOfWeek.TUESDAY or Month.FEBRUARY.

    The java.time framework also includes classes such as MonthDay, YearMonth, and Year. Again, you can pass full-fledged objects rather than mere numbers or strings to make your code more self-documenting, ensure valid values, and provide type-safety.

    Converting between TimeUnit and ChronoUnit

    We can easily convert between TimeUnit and ChronoUnit. See the new methods added to the older class, TimeUnit.

    • TimeUnit.of( ChronoUnit )
    • TimeUnit::toChronoUnit()

    ThreeTen-Extra project

    The ThreeTen-Extra project provides additional classes to work with java.time. These include: DayOfMonth, DayOfYear, AmPm, Quarter, YearQuarter, YearWeek, Days, Weeks, Months, Years, and Interval.

    0 讨论(0)
提交回复
热议问题