Solved

Java

Posted on 2007-11-20
4
210 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 109
map related example 6 49
web application structure 18 77
Why my table column Id is not passed to java object? 4 39
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

831 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