[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Java

Posted on 2007-11-20
4
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Assisted Solution

by:willyRD
willyRD earned 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

656 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