Calendar class time month not matching

后端 未结 3 366
无人及你
无人及你 2021-01-22 00:27
Calendar c = Calendar.getInstance();

System.out.println("Mili->>" + c.getTimeInMillis());

System.out.println("Month  ->>" + Calendar.MO         


        
相关标签:
3条回答
  • 2021-01-22 00:46

    You're getting calendar c from an instance but calendar from month with the Calendar object, get it from the instance too.

    Also - Calendar starts from 0 as referenced here

    In other news, use the new DateTime API in Java 8 or JODA time, they're much nicer.

    0 讨论(0)
  • 2021-01-22 00:53

    What you want is the following idiom: c.get(Calendar.MONTH).

    Calendar.MONTH per se is just an internal constant and will (hopefully) always return 2.

    Example

    // We're in June at the time of writing
    // Months are 0-based so June == 5
    Calendar c = Calendar.getInstance();
    System.out.println(Calendar.MONTH);
    System.out.println(c.get(Calendar.MONTH));
    

    Output

    2
    5
    

    See also: API

    0 讨论(0)
  • 2021-01-22 01:01

    You are displaying the value of the Calendar.MONTH constant, hence 2.

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