August 18 2016

Java Date Subtract Days

Java Date Subtract Days

Below example subtracts 20 days from the date

package com.answersz;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class JavaDateSubtractDays {
 
public static void main(String[] args) throws ParseException {
 
// Java Date Subtract Days
 
String date = "17-08-2016";
 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
 Date dt = sdf.parse(date);
 
Calendar calendar = Calendar.getInstance();
 calendar.setTime(dt);
 
// Number of dates to Subtract
 calendar.add(Calendar.DATE, -20);
 
Date dateAfterSubtract = calendar.getTime();
 System.out.println("Date : " + sdf.format(dt));
 System.out.println("New Date : " + sdf.format(dateAfterSubtract));
 }
}

Ouput

Date : 17-08-2016
New Date : 28-07-2016


Copyright © 2014. All rights reserved.

Posted August 18, 2016 by answersz in category Core Java

Leave a Comment

Your email address will not be published. Required fields are marked *

*