Solved

Java Date Format Doubt

Posted on 2011-09-19
13
494 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

696 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