How to initialize Gregorian calendar with date as YYYY-MM-DD format?

后端 未结 2 1627
北海茫月
北海茫月 2021-01-28 08:49

I have a class which contains a variable

private GregorianCalendar cal;

Now I want to initialize it with a date in yyyy-mm-dd

相关标签:
2条回答
  • 2021-01-28 09:01

    First, you may hate me for reiterating, you need to understand that there’s a difference between a date and a string representation of that date; a difference between the fundamental data and a string representation of it.

    Second, you can use LocalDate (and the other Java date and time classes) with Java 7 if you want. It is (and they are all) in the ThreeTen-Backport , a back-port of the Java SE 8 date-time classes to Java SE 6 and 7.

    Since I gather that LocalDate fits your requirements much better, I really think you should give it a thought or two. So instead of your cal I suggest

    private LocalDate date = LocalDate.now(ZoneId.systemDefault());
    

    Also think about whether you want the current time zone setting of your JVM (as the above will give you) or you want to control which time zone you use. It will make a difference.

    Finally, if you really insist. As you have understood by now, I cannot give you a string in a GregorianCalendar. You may discard the time part of your GregorianCalendar so you only have the date part. And you may format it into a string of your liking.

    public class GregorianCalendarDemo {
    
        private GregorianCalendar cal;
    
        public GregorianCalendarDemo() {
            cal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
            // discard time of day so we only have the date
            cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
            cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        }
    
        protected GregorianCalendar getCal() {
            return cal;
        }
    
        public String getFormattedCal() {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(getCal().getTime());
        }
    }
    

    When I just called getFormattedCal(), it returned 2017-05-25.

    Again, decide whether default values for time zone and locale are fine or you want something else.

    You might have thought that we could discard the hours with just cal.set(Calendar.HOUR_OF_DAY, 0):, and similarly with minutes and seconds. It would work in 99 % of all cases at least. However, with transistion to summer time (daylight savings time), the day is not guaranteed to begin at 0 hours, so the above code is more bulletproof.

    Link

    ThreeTen Backport home

    0 讨论(0)
  • 2021-01-28 09:20

    You can try to use SimpleDateFormat for example :

    GregorianCalendar cal = new GregorianCalendar();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    format.setCalendar(cal);
    String result = format.format(cal.getTime());
    System.out.println(result);//today is 2017-05-25
    

    To convert String to GregorianCalendar :

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssX");
    Date date = format.parse("2017-05-25T14:36:52+03:00");
    GregorianCalendar cal2 = new GregorianCalendar();
    cal2.setTime(date);
    
    0 讨论(0)
提交回复
热议问题