java.util.Date and getYear()

前端 未结 12 920
生来不讨喜
生来不讨喜 2020-12-08 13:01

I am having the following problem in Java (I see some people are having a similar problem in JavaScript but I\'m using Java)

System.out.println(new Date().ge         


        
相关标签:
12条回答
  • 2020-12-08 13:38

    There are may ways of getting day, month and year in java.

    You may use any-

        Date date1 = new Date();
        String mmddyyyy1 = new SimpleDateFormat("MM-dd-yyyy").format(date1);
        System.out.println("Formatted Date 1: " + mmddyyyy1);
    
    
    
        Date date2 = new Date();
        Calendar calendar1 = new GregorianCalendar();
        calendar1.setTime(date2);
        int day1   = calendar1.get(Calendar.DAY_OF_MONTH);
        int month1 = calendar1.get(Calendar.MONTH) + 1; // {0 - 11}
        int year1  = calendar1.get(Calendar.YEAR);
        String mmddyyyy2 = ((month1<10)?"0"+month1:month1) + "-" + ((day1<10)?"0"+day1:day1) + "-" + (year1);
        System.out.println("Formatted Date 2: " + mmddyyyy2);
    
    
    
        LocalDateTime ldt1 = LocalDateTime.now();  
        DateTimeFormatter format1 = DateTimeFormatter.ofPattern("MM-dd-yyyy");  
        String mmddyyyy3 = ldt1.format(format1);  
        System.out.println("Formatted Date 3: " + mmddyyyy3);  
    
    
    
        LocalDateTime ldt2 = LocalDateTime.now();
        int day2 = ldt2.getDayOfMonth();
        int mont2= ldt2.getMonthValue();
        int year2= ldt2.getYear();
        String mmddyyyy4 = ((mont2<10)?"0"+mont2:mont2) + "-" + ((day2<10)?"0"+day2:day2) + "-" + (year2);
        System.out.println("Formatted Date 4: " + mmddyyyy4);
    
    
    
        LocalDateTime ldt3 = LocalDateTime.of(2020, 6, 11, 14, 30); // int year, int month, int dayOfMonth, int hour, int minute
        DateTimeFormatter format2 = DateTimeFormatter.ofPattern("MM-dd-yyyy");  
        String mmddyyyy5 = ldt3.format(format2);   
        System.out.println("Formatted Date 5: " + mmddyyyy5); 
    
    
    
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(new Date());
        int day3  = calendar2.get(Calendar.DAY_OF_MONTH); // OR Calendar.DATE
        int month3= calendar2.get(Calendar.MONTH) + 1;
        int year3 = calendar2.get(Calendar.YEAR);
        String mmddyyyy6 = ((month3<10)?"0"+month3:month3) + "-" + ((day3<10)?"0"+day3:day3) + "-" + (year3);
        System.out.println("Formatted Date 6: " + mmddyyyy6);
    
    
    
        Date date3 = new Date();
        LocalDate ld1 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date3)); // Accepts only yyyy-MM-dd
        int day4  = ld1.getDayOfMonth();
        int month4= ld1.getMonthValue();
        int year4 = ld1.getYear();
        String mmddyyyy7 = ((month4<10)?"0"+month4:month4) + "-" + ((day4<10)?"0"+day4:day4) + "-" + (year4);
        System.out.println("Formatted Date 7: " + mmddyyyy7);
    
    
    
        Date date4 = new Date();
        int day5   = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date4)).getDayOfMonth();
        int month5 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date4)).getMonthValue();
        int year5  = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date4)).getYear();
        String mmddyyyy8 = ((month5<10)?"0"+month5:month5) + "-" + ((day5<10)?"0"+day5:day5) + "-" + (year5);
        System.out.println("Formatted Date 8: " + mmddyyyy8);
    
    
    
        Date date5 = new Date();
        int day6   = Integer.parseInt(new SimpleDateFormat("dd").format(date5));
        int month6 = Integer.parseInt(new SimpleDateFormat("MM").format(date5));
        int year6  = Integer.parseInt(new SimpleDateFormat("yyyy").format(date5));
        String mmddyyyy9 = ((month6<10)?"0"+month6:month6) + "-" + ((day6<10)?"0"+day6:day6) + "-" + (year6);
        System.out.println("Formatted Date 9: " + mmddyyyy9);
    
    0 讨论(0)
  • 2020-12-08 13:40

    In addition to all the comments, I thought I might add some code on how to use java.util.Date, java.util.Calendar and java.util.GregorianCalendar according to the javadoc.

    //Initialize your Date however you like it.
    Date date = new Date();
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    //Add one to month {0 - 11}
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    
    0 讨论(0)
  • 2020-12-08 13:42

    tl;dr

    LocalDate.now()       // Capture the date-only value current in the JVM’s current default time zone.
             .getYear()   // Extract the year number from that date.
    

    2018

    java.time

    Both the java.util.Date and java.util.Calendar classes are legacy, now supplanted by the java.time framework built into Java 8 and later.

    A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

    If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

    Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

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

    If you want only the date without time-of-day, use LocalDate. This class lacks time zone info but you can specify a time zone to determine the current date.

    ZoneId zoneId = ZoneId.of( "America/Montreal" );
    LocalDate localDate = LocalDate.now( zoneId );
    

    You can get the various pieces of information with getYear, getMonth, and getDayOfMonth. You will actually get the year number with java.time!

    int year = localDate.getYear();
    

    2016

    If you want a date-time instead of just a date, use ZonedDateTime class.

    ZonedDateTime zdt = ZonedDateTime.now( zoneId ) ;
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

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

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

    Much of the java.time functionality is back-ported to Java 6 & Java 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
  • 2020-12-08 13:42

    This behavior is documented in the java.util.Date -class documentation:

    Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

    It is also marked as deprecated. Use java.util.Calendar instead.

    0 讨论(0)
  • 2020-12-08 13:45

    Use date format

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = format.parse(datetime);
    SimpleDateFormat df = new SimpleDateFormat("yyyy");
    year = df.format(date);
    
    0 讨论(0)
  • 2020-12-08 13:45
            try{ 
    int year = Integer.parseInt(new Date().toString().split("-")[0]); 
    } 
    catch(NumberFormatException e){
    }
    

    Much of Date is deprecated.

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