aspose file tools*
The moose likes Java in General and the fly likes add minutes to date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "add minutes to date" Watch "add minutes to date" New topic
Author

add minutes to date

vittal mareddy
Greenhorn

Joined: Jul 24, 2011
Posts: 16
I have date in string variable, i have to add/subtract minutes from it, please adivce , how i could do in java 1.5
sample data
String olddate = "12/16/2010 4:58:29 PM"
String minutes1 = "300"
i would like to get value as below for above data
String newdate ="12/16/2010 9:58:29 PM"


Thanks
vittal

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try the Calendar class
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2232
    
  47
You will first need to parse the String to create a java.util.Date object which you can do via java.text.DateFormat. Then create your Calendar object and set the date/time using the Date object. Do the time adjustment and reverse the process to get back to your formatted string.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7625
    
  19

Michael Dunn wrote:try the Calendar class

@vittal: And specifically, its add(field, amount) method.

And be very careful not to misinterpret what you get back as a String, because there are many possibilities for why it might "look" wrong - the most obvious of which is a Daylight Savings change.

Winston
vittal mareddy
Greenhorn

Joined: Jul 24, 2011
Posts: 16
It worked using dateformat & calender, but as Winston mentioned we do see issue with daylight saying, please suggest how to handle.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2232
    
  47
What issue do you see? Is it not handling daylight saving when you expected it to or visa versa or did you not want to handle daylight saving at all or ...

The string representing a particular Date object's value is relative to it's timezone so it may be that you are not using the correct timezone for the area you are in.
vittal mareddy
Greenhorn

Joined: Jul 24, 2011
Posts: 16
Users are accessing web application from different countries, application is deployed in Germany. code is getting time in germany time zone, in application getting timezone offset(difference server to client) adding / subtracting to server time. looks due to daylight saving for some records time show difference than local time expected.

code i have is, it doesn't handle daylight saving, if i want to handle also daylight saving for different countries, what i have to do.

getLocalTime(String timeInServer, int minutes){
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String newdate = null;

Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(timeInServer));
cal.add(Calendar.MINUTE, minutes);
newdate = sdf.format(cal.getTime());
return newdate;
}
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2232
    
  47
I'm not sure I fully understand what you are trying to do. If it is to display the server time in the local time then can't you just return the servers current time as a Date object (or as the number of milliseconds from epoch) and then in the client use a formatter with the timezone set to the local zone to get the String representation from the Date object.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7625
    
  19

vittal mareddy wrote:Users are accessing web application from different countries, application is deployed in Germany. code is getting time in germany time zone, in application getting timezone offset(difference server to client) adding / subtracting to server time.

Well I suspect that's you're problem right there.

Don't start mucking about with the time you've got - and if that was the reason for your question in the first place, then your entire premise is WRONG.

Just display the time according to the Locale setting for the machine - the same time value everywhere.

Winston
vittal mareddy
Greenhorn

Joined: Jul 24, 2011
Posts: 16
Ok, from Tony comment, modified my code as below

getLocalTime(String timeInServer, int timezone){
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String newdate = null;
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(date));
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String newdate = sdf.format(cal.getTime());
System.out.println("before = "+date);
System.out.println("after = "+newdate);
return newdate;
}

I have hard coded timezone value as America/Los_Angeles to get user TimeZone object. my question here is how i can find time zone value which i have hard coded(America/Los_Angeles), is there any java script method available or httprequest will have.
thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7625
    
  19

vittal mareddy wrote:I have hard coded timezone value as America/Los_Angeles to get user TimeZone object. my question here is how i can find time zone value which i have hard coded(America/Los_Angeles), is there any java script method available or httprequest will have.

My advice: StopCoding (←click) and explain exactly what you're trying to achieve.

It looks to me as though you're flailing around at the moment; and I suspect also that you're also labouring under some misapprehensions about how time works in Java.

If I create a time value using System.currentTimeMillis(), and I send that value to each of my machines around the world and use it to display a time on them using the standard Calendar methods, then each machine will display a different CLOCK time. That is to say, they will display the same time, but according to their own Locale (java.util.Locale), which includes their timezone (and, possibly, DST).

I suspect you're overthinking this by a long way, because you do NOT need to change anything in order to get the correct time displayed in different countries.

Winston
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2232
    
  47
Ok, from Tony comment, modified my code as below

That's not what I said to do.
Winston and I are saying the same thing about how to handle the time. I think Winston explained better than I did and I certainly agree with his advice about stop coding until you fully understand the way dates and time works in Java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: add minutes to date