Reputation: 11
i am fetching date from Oracle database using rp.getStart_date() with hibernate now i want to add 15 days to it and display it. but as cal.add() requires first argument as int,it is showing me numberFormatException on second line..how do i do it?
Date dt=rp.getStart_date();
int s1=Integer.parseInt((dt.toString()));
System.out.println(s1);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
Calendar cal=Calendar.getInstance();
cal.add(s1, 15);
System.out.println(sdf.format(cal.getTime()));
please help me out..
Thanks in advance..
Upvotes: 0
Views: 820
Reputation: 338730
The answer by Rakesh KR is close but not quite right. Both the question and that answer fail to think about time zone. If you neglect to specify a time zone, you get the JVM's default time zone. By relying on default time zone, you may have unexpected results.
The add
method of java.util.Calendar retains the hour-of-day, adjusting for Daylight Saving Time (DST) and possibly other anomalies. So if you use a time zone (like United States west coast) that changes Daylight Saving Time by an hour during your time span, you actually will have one extra/less hour. That is, your result in hours of adding 15 days is ( ( 15 * 24 ) ± 1 )
.
If you were expecting ( 15 * 24 )
consistently, you will be surprised (depending default time zone of JVM).
Since the plusDays
method on DateTime class in Joda-Time has the same behavior, I'll demonstrate using Joda-Time. You should be avoiding java.util.Date & java.util.Calendar anyways, but in this scenario the behavior is the same as Joda-Time.
First, for your information, the code from the other answer could be done Joda-Time like this, converting to-and-fro between the java.util.Date world and Joda-Time world. But this code has the same time zone issue (affected by DST).
java.util.Date juDate = new java.util.Date();
java.util.Date juDateLater = new DateTime( juDate ).plusDays( 15 ).toDate() ;
Now using pure Joda-Time let's look at how time zone affects the addition of days. We will run this example code twice, first as-is, then swapping time zones by commenting out the first timeZone
line and enabling the line after that.
// Time zone "America/Los_Angeles" begins DST on 2014-03-09 02:00, springing ahead to 03:00.
DateTimeZone timeZone = DateTimeZone.forID( "America/Los_Angeles" );
//DateTimeZone timeZone = DateTimeZone.UTC;
DateTime dateTime_OneAM = new DateTime( 2014, 3, 9, 1, 0, 0, timeZone );
DateTime dateTime_OneAM_Plus15 = dateTime_OneAM.plusDays( 15 );
DateTime dateTime_ThreeAM = new DateTime( 2014, 3, 9, 3, 0, 0, timeZone );
DateTime dateTime_ThreeAM_Plus15 = dateTime_ThreeAM.plusDays( 15 );
long millisElapsedOneAM = ( dateTime_OneAM_Plus15.getMillis() - dateTime_OneAM.getMillis() );
long millisElapsedThreeAM = ( dateTime_ThreeAM_Plus15.getMillis() - dateTime_ThreeAM.getMillis() );
long minutes = ( ( millisElapsedThreeAM - millisElapsedOneAM ) / 1000L / 60L );
Dump to console…
System.out.println( "timeZone " + timeZone );
System.out.println( "dateTime_OneAM " + dateTime_OneAM + " ( UTC/GMT: " + dateTime_OneAM.toDateTime( DateTimeZone.UTC ) + " )" );
System.out.println( "dateTime_OneAM_Plus15 " + dateTime_OneAM_Plus15 + " ( UTC/GMT: " + dateTime_OneAM_Plus15.toDateTime( DateTimeZone.UTC ) + " )" );
System.out.println( " " ); // Blank line.
System.out.println( "dateTime_ThreeAM " + dateTime_ThreeAM + " ( UTC/GMT: " + dateTime_ThreeAM.toDateTime( DateTimeZone.UTC ) + " )" );
System.out.println( "dateTime_ThreeAM_Plus15 " + dateTime_ThreeAM_Plus15 + " ( UTC/GMT: " + dateTime_ThreeAM_Plus15.toDateTime( DateTimeZone.UTC ) + " )" );
System.out.println( " " ); // Blank line.
System.out.println( "millisElapsedOneAM " + millisElapsedOneAM );
System.out.println( "millisElapsedThreeAM " + millisElapsedThreeAM );
System.out.println( "minutes " + minutes );
When run using the first time zone, for US west coast…
(Note how hour-of-day in UTC changing or not changing)
timeZone America/Los_Angeles
dateTime_OneAM 2014-03-09T01:00:00.000-08:00 ( UTC/GMT: 2014-03-09T09:00:00.000Z )
dateTime_OneAM_Plus15 2014-03-24T01:00:00.000-07:00 ( UTC/GMT: 2014-03-24T08:00:00.000Z )
dateTime_ThreeAM 2014-03-09T03:00:00.000-07:00 ( UTC/GMT: 2014-03-09T10:00:00.000Z )
dateTime_ThreeAM_Plus15 2014-03-24T03:00:00.000-07:00 ( UTC/GMT: 2014-03-24T10:00:00.000Z )
millisElapsedOneAM 1292400000
millisElapsedThreeAM 1296000000
minutes 60
When run using the second time zone, for UTC/GMT…
timeZone UTC
dateTime_OneAM 2014-03-09T01:00:00.000Z ( UTC/GMT: 2014-03-09T01:00:00.000Z )
dateTime_OneAM_Plus15 2014-03-24T01:00:00.000Z ( UTC/GMT: 2014-03-24T01:00:00.000Z )
dateTime_ThreeAM 2014-03-09T03:00:00.000Z ( UTC/GMT: 2014-03-09T03:00:00.000Z )
dateTime_ThreeAM_Plus15 2014-03-24T03:00:00.000Z ( UTC/GMT: 2014-03-24T03:00:00.000Z )
millisElapsedOneAM 1296000000
millisElapsedThreeAM 1296000000
minutes 0
Upvotes: 0
Reputation: 6527
Try,
Date dt = rp.getStart_date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
cal.add(Calendar.DATE, 15); // Add 15 days
String output = sdf.format(cal.getTime());
System.out.println("Output :: "+output);
Upvotes: 2