开发者

How to get Century from date in Java

开发者 https://www.devze.com 2023-03-10 10:30 出处:网络
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 current century from this date using SimpleDateF

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 current century from this date using SimpleDateFormat?


Date date = new SimpleDateFormat("MM/dd/yyyy").parse(yourString);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

int century = (calendar.get(Calendar.YEAR) / 100) +1;


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;
    }

}


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


int century =  (year + 99)/ 100;


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!


You would simply return (year + 99) / 100


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号