java - Parse Time into Calendar without losing Date part -
i need store in calendar
java object date/time consisting of today's date plus time parsed string in hh:mm:ss
format.
i tried creating new gregoriancalendar
, using settime()
initialize result of calling simpledateformat
's parse() method noticed parse()
returns me date
object correct parsed time date of jan 1, 1970.
for example:
string time = "23:32"; string timefmt = new simpledateformat("hh:mm"); calendar todaystime = new gregoriancalendar(); todaystime.settime(timefmt.parse(time)); system.out.println(todaystime.gettime());
prints: thu jan 01 11:32:00 cst 1970
expected was: wed 03 11:32:00 cst 2015
i can see 1 alternative: create second gregoriancalendar
todays date , set year, month , day manually todaystime
via setyear()
, setmonth()
, setdayofmonth()
hope there more elegant way that.
can me better way parse time part parsed date
object without losing date part?
you can this:
date parseddate = simpledateformat.getinstance().parse(time); calendar instance = calendar.getinstance(); instance.settime( parseddate ); calendar result = calendar.getinstance(); result.set( calendar.hour_of_day, instance.get( calendar.hour_of_day ) ); result.set( calendar.minute, instance.get( calendar.minute ) );
Comments
Post a Comment