How to get Century from date in Java

前端 未结 5 899
孤独总比滥情好
孤独总比滥情好 2021-01-04 20:41

How to get current Century from a date in Java?

For example the date \"06/03/2011\" according to format \"MM/dd/yyyy\". How can I get curre

相关标签:
5条回答
  • 2021-01-04 21:16

    The other Answers are correct but outdated.

    java.time

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

    Now in maintenance mode, the Joda-Time project also advises migration to java.time.

    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 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

    LocalDate

    The LocalDate class represents a date-only value without time-of-day and without time zone.

    To parse specify a formatting pattern. By the way, I suggest using ISO 8601 standard formats which can be parsed directly by java.time classes.

    String input = "06/03/2011";
    
    DateTimeFormatter f = DateTimeFormatter.ofPattern ( "MM/dd/uuuu" ).withLocale ( Locale.US );
    LocalDate ld = LocalDate.parse ( input , f );
    

    To get the century, just take the year number and divide by 100. If you want the ordinal number, "twenty-first century" for 20xx, add one.

    int centuryPart = ( ld.getYear () / 100 );
    int centuryOrdinal = ( ( ld.getYear () / 100 ) + 1 );
    

    Dump to console.

    System.out.println ( "input: " + input + " | ld: " + ld + " | centuryPart: " + centuryPart + " | centuryOrdinal: " + centuryOrdinal );
    

    input: 06/03/2011 | ld: 2011-06-03 | centuryPart: 20 | centuryOrdinal: 21

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

    Split it by the slahes, get the first two symbols of the third element in the resulting array, Integer.parseInt it and add 1, that is:

    String arr = myDate.split("/");
    String shortYear = myDate[2].substring(0, 2);
    int century = Integer.parseInt(shortYear) + 1;
    
    (not sure about the substring() syntax off the top of my head)

    0 讨论(0)
  • 2021-01-04 21:23
    Date date = new SimpleDateFormat("MM/dd/yyyy").parse(yourString);
    
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    
    int century = (calendar.get(Calendar.YEAR) / 100) +1;
    
    0 讨论(0)
  • 2021-01-04 21:33

    I dont know anything about Java but why don't you just get the full year and make the last 2 digits 0?

    EDIT

    If you want 2011 to become 21st century - just get the fully qualified year in string format, then knock off the last 2 characters, then parse to an int and add 1!

    0 讨论(0)
  • 2021-01-04 21:38

    A slight change to what Harry Lime posted. His logic is not entirely correct. Year 1901 would be 20th century, but 1900 would be 19th century.

    public class CenturyYear {
    
        public static void main(String[] args) {
            int test = centuryFromYear(1900);
            System.out.println(test);
    
        }
    
        static int centuryFromYear(int year) {
            if (year % 100 == 0) {
                year = year / 100;
            } else {
                year = (year / 100) + 1;
            }
            return year;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题