Solved

Java

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
eclipse package explorer vs project explorer view 2 84
java jdbc batch example 8 22
tomcat administrtor 12 32
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now