Java - Subtract Days from date [duplicate]

2020-01-29 18:13发布

问题:

I'm trying to subtract 5 days from a date which comes in as a string initially.

I have had a look at some of the other posts on this subject but the result i get from the code is always incorrect. The main problem is that the year value does not seem to change when the days are subtracted for example - 2012-01-01 subtract 5 days gives me 'Jan 27 2012' using this code -

cal.add(Calendar.DATE, -5);

Please help.

回答1:

Did you know that, in Java, month 1 is actually February?

Date februaryTheFirst = new Date(2012,1,1); // equals 2012-02-01

This might explain what you are seeing. If you want to instantiate 2012-01-01 instead, you should do:

Date firstDayOf2012 = new Date(2012,0,1); // this is 2012-01-01

Exactly the same thing happens when dealing with Calendar:

Calendar.getInstance().set(2012,0,1); // 2012-01-01

Be sure to check the documentation for Date(int, int, int) and Calendar.set(int, int, int). Also, you could check the way you are parsing the string. If you use SimpleDateFormat.parse(...), things can be easier.

Strange, isn't it? Go figure... Just as a fun fact, IntelliJ's documentation annotates this second parameter, month, with @MagicConstant, to remember the programmer that there's something very strange going on.



回答2:

Calendar.FEBRUARY is 1 and five days before 1 Feb 2012 was 27 Jab 2012.



回答3:

Your implementation is correct and you are getting the correct value aslo.

Calendar's Months started with 0

0 = Jan
1 = Feb

so subtracting 5 days from 2012-01-01 will definitely returns you Jan 27 2012 something is here also which will helps you Why is January month 0 in Java Calendar?



回答4:

Joda-Time

The Joda-Time 2.7 library makes this work much easier. Just call the minusDays method.

String input = "2012-01-01";
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime dateTime = new DateTime( input, zone );
DateTime then = now.minusDays( 5 );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withZone( zone ).withLocale( Locale.CANADA_FRENCH );
String output = formatter.print( then );

If you want the beginning of the day, add a call to withTimeAtStartOfDay. This is unnecessary in your case, when parsing a date-only string with no time-of-day.

DateTime dateTimeAtStartOfDay = new DateTime( input, zone ).withTimeAtStartOfDay();

If you want only date without time-of-day or time zone, use LocalDate instead of DateTime.

LocalDate then = new LocalDate( "2012-01-01" ).minusDays( 5 );

If you need to convert to the old java.util.Date, call toDate on the DateTime.

java.time

Java 8 has a new package, java.time. These new classes were inspired by Joda-Time but were re-architected. Both java.time and Joda-Time can solve this particular problem equally well.



回答5:

Use:

cal.add(Calendar.DAY_OF_MONTH, -5)

EDIT: sorry. DAY_OF_MONTH is a synonym to DATE. Instead of 1 use Calendar.JANUARY.



回答6:

This a segment of code that is working on my pc. first you have to get the calendar instance the perform your calculation.

            Calendar cal = Calendar.getInstance();
     System.out.println("Today : " + cal.getTime());
    // Subtract 300 days from the calendar
    cal.add(Calendar.DATE, -300);
    System.out.println("300 days ago: " + cal.getTime());

This is the output that you will get:

    Today : Wed Oct 17 10:41:23 EET 2012
    300 days ago: Thu Dec 22 10:41:23 EET 2011