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

x
?
Solved

Java Date Format Doubt

Posted on 2011-09-19
13
Medium Priority
?
504 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:Gurvinder Pal Singh
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Independent Software Vendors: 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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