java - Unable to Parse Time String to specified format -


i need compare 2 time string have entered.. able date time unable it. please find below code tried :

my issue is.. -- able time format according format enter in string unable parse it. because if parse able compare , find out whether varible time field difference. please find below code have done :

try{ dateformat df = new simpledateformat(format); 

//assume format entered hh:mm:ss

    df.setlenient(false);                  t1 = new date();  //here current time                 temp_time = df.format(t1);  

// change format have requested (example format – hh:mm:ss. temp_time = 18:20:45).

                t1 = df.parse(temp_time);  

// when try parse thu jan 01 13:24:40 ist 1970. able parse date, time format it’s not working.

in order see how parse-result looks (an object of type java.util.date) have format again:

dateformat df = new simpledateformat("hh:mm:ss"); date time = df.parse("18:20:45");  // visualizing result system.out.println(df.format(time)); // output: 18:20:45 

you cannot rely on system.out.println(time) because code uses output of tostring() of java.util.date dependent on system timezone , prints whole timestamp based on date 1970-01-01 date offset (which used simpledateformat due lack of date information in input string).

for comparing time strings should use chronologically descending order (from left right) in iso-8601-format hh:mm:ss here string comparison sufficient.

update because of comment:

if concern use parsing compare time objects still think string comparison of iso-formatted time strings simple , straight-forward option because can leave parsing effort completely. iso-8601 has defined time format "hh:mm:ss" respect ordering requirements.

if have more plans/intentions beyond simple comparisons can of course parse time strings date-objects , compare these parsed objects on basis of method gettime() in comment , other additional stuff (ugly , half-deprecated) date-objects.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -