Solved

Java

Posted on 2007-11-20
4
211 Views
Last Modified: 2013-11-23
how to subtract 2 days from a date and format it in java without using deprecated functions?
0
Comment
Question by:chisun7679
4 Comments
 
LVL 3

Assisted Solution

by:willyRD
willyRD earned 250 total points
ID: 20320758
A possible solution. You can replace currentDate for the Date you want to substract two days from and change the format for the date in strFormat.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class SubstractFromDate {

      /**
       * @param args
       */
      public static void main(String[] args) {
            Calendar date = new GregorianCalendar();
            String strFormat = "MM/dd/yyyy";
            Date currentDate = new Date(); //current date
            DateFormat myDateFormat = new SimpleDateFormat(strFormat);
            
            System.out.println("Start date: "+date.toString());
            
            date.setTime(currentDate);
            date.add(Calendar.DAY_OF_MONTH,-2); //substract 2 days from date
            
       
        try {
              System.out.println("Non formatted date: "+date.toString());
              String formattedDate = myDateFormat.format(date.getTime());
              System.out.println("Formatted date: "+formattedDate);
            } catch (Exception e) {
            System.out.println("Error formatting date: "+e);
            e.printStackTrace();
       }
      }

}
0
 
LVL 16

Accepted Solution

by:
imladris earned 250 total points
ID: 20320977
GregorianCalendar gc=new GregorianCalendar(2007,0,1); // 1 Jan 2007
gc.add(Calendar.DAY_OF_MONTH,-2);  // subtract two days
SimpleDateFormat sdf=new SimpleDateFormat();  // date formatter
String date=sdf.format(gc.getTime());  // format Date returned from getTime to string

API documentation can be found at: http://java.sun.com/javase/6/docs/api/

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question