Solved

Java Date Format Doubt

Posted on 2011-09-19
13
493 Views
Last Modified: 2012-05-12
Hi Experts...
I have two date formats:Sep  19 2011  9:03:32:000AM and 09/19/11 how do i generate these date format in java.
How do i perform addition to these date formats like adding 4 days to 'Sep  19 2011  9:03:32:000AM' .
Please help...
0
Comment
Question by:gaugeta
13 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36559240
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36559243
Date date= new Date();
formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss");
s = formatter.format(date);

for date formate and for addition
..........................................................................

 Calendar now = Calendar.getInstance();
 
    //add days to current date using Calendar.add method
    now.add(Calendar.DATE,1);
0
 
LVL 1

Expert Comment

by:Gyufa
ID: 36559269
Hi!

There is a BSD licensed java library called Date4J, you should give it a try:

http://www.date4j.net/#Examples

It's pretty easy to use. The javadoc is located here:

http://www.date4j.net/javadoc/hirondelle/date4j/DateTime.html
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:gaugeta
ID: 36559393
@nishantcomp2512:Thanks for replying.
Now how do i add days to the format created in the program.
Please help...
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class sample
{
	public static void main(String args[])
	{
		Date date= new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss:sss a");
		System.out.println(formatter.format(date));
		 Calendar now = Calendar.getInstance();
		 now.add(Calendar.DATE,1);
		 System.out.println(now.toString());

	}
}

Open in new window

0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36559403
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class sample
{
      public static void main(String args[])
      {
                int noOfDays=4;
            Date date= new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss:sss a");
            System.out.println(formatter.format(date));
             Calendar now = Calendar.getInstance();
             now.add(Calendar.DATE,noOfDays);
             System.out.println(now.toString());

      }

if you want eo subtract then noOfDay(-4)....
tks..
}
0
 

Author Comment

by:gaugeta
ID: 36559411
@nishantcomp2512:Yes i tried this.
I have to get the date in the pattern "MMM dd yyyy HH:mm:ss:sss a" after addition.
Please help...
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36559428
ok..
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class sample
{
      public static void main(String args[])
      {
                int noOfDays=4;
            Date date= new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss:sss a");
            System.out.println(formatter.format(date));
             Calendar now = Calendar.getInstance();
             now.add(Calendar.DATE,noOfDays);
             System.out.println(formatter.format(now));

      }

}

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36559445
@nishantcomp2512:Yes ive tried this too.
Its giving an illegal argument exception.
Please help...
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
	at java.text.DateFormat.format(Unknown Source)
	at java.text.Format.format(Unknown Source)
	at sample.main(sample.java:15)

Open in new window

0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36559461
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class sample
{
      public static void main(String args[])
      {
                int noOfDays=4;
            Date date= new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss:sss a");
            System.out.println(formatter.format(date));
             Calendar now = Calendar.getInstance();
             now.add(Calendar.DATE,noOfDays);
             System.out.println(formatter.format(now.Date));

      }

}
try now

simpleDateFormate require Date object and DateFormate require calendet object...
0
 

Author Comment

by:gaugeta
ID: 36559489
@nishantcomp2512:The program returns a compleler error on line:-   System.out.println(formatter.format(now.Date));
And after correction:-
System.out.println(formatter.format(now.DATE));
We get  Jan 01 1970 05:30:00:000 AM as the output.
Please help...
0
 
LVL 14

Accepted Solution

by:
nishant joshi earned 500 total points
ID: 36559523


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class sample
{
      public static void main(String args[])
      {
                int noOfDays=4;
            Date date= new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss:sss a");
            System.out.println(formatter.format(date));
             Calendar now = Calendar.getInstance();
             now.add(Calendar.DATE,noOfDays);
             System.out.println(formatter.format(now.get(Calendar.DATE)));

      }

}
0
 

Author Comment

by:gaugeta
ID: 36559538
@nishantcomp2512:Thanks anyway i got the answer.
Its System.out.println(formatter.format(now.get(Calendar.getTime())));


0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36559549
	public static void main(String args[]) {
		int noOfDays = 4;
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(" MMM dd yyyy HH:mm:ss:sss a");
		System.out.println(formatter.format(date));
		Calendar now = Calendar.getInstance();
		now.add(Calendar.DATE, noOfDays);
		System.out.println(formatter.format(now.getTime()));

	}

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
How to convert String matching to regex in java 4 51
servlet and mdb, jms error 1 37
Eclipse for Java EE development 2 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

827 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