Solved

Java Date Format Doubt

Posted on 2011-09-19
13
490 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 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

12 Experts available now in Live!

Get 1:1 Help Now