How to get Century from a date in Java

How to get current Century from a date in Java?

For example, the date is "06/03/2011" in accordance with the format "MM/dd/yyyy" . How can I get the current century from this date using SimpleDateFormat ?

+7
source share
5 answers
 Date date = new SimpleDateFormat("MM/dd/yyyy").parse(yourString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int century = (calendar.get(Calendar.YEAR) / 100) +1; 
+9
source

A slight change to what Harry Lime published. His logic is not entirely correct. 1901 will be the 20th century, but 1900 will be the 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; } } 
+8
source

Other answers are correct but outdated.

java.time

The java.time framework is built into Java 8 and later. These classes supersede old inconvenient time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .

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

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

Most of the functionality of java.time is ported to Java 6 and 7 in ThreeTen-Backport and further adapted for Android in ThreeTenABP .

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time.

LocalDate

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

To parse, specify a formatting template. By the way, I suggest using standard ISO 8601 formats, which can be analyzed directly by the 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 a century, just take the year number and divide it by 100. If you want a serial number , "twenty first century", for 20xx add one.

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

Dump for the console.

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

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

+2
source

I don't 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 be the 21st century, just get a fully qualified year in a lowercase format, then knock down the last 2 characters, then parse the int and add 1!

+1
source

Divide it by slahes, get the first two characters of the third element in the resulting Integer.parseInt array 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 syntax of the substring () from the top of the head)
0
source

All Articles