Solved

Java

Posted on 2007-11-20
4
206 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 47
Understanding websocket example in spring 1 41
for i loop in grovy 1 32
eclipse formatting 6 38
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

771 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

11 Experts available now in Live!

Get 1:1 Help Now