Time consts in Java?

前端 未结 13 603
遇见更好的自我
遇见更好的自我 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

    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..

提交回复
热议问题