Solved

Java Date Format Doubt

Posted on 2011-09-19
13
495 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

688 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