• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

add minutes to date

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try the Calendar class
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked using dateformat & calender, but as Winston mentioned we do see issue with daylight saying, please suggest how to handle.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic