java - Parse date/time from string? -


i'm trying create program parse meaningful date , time string. want able give following kinds of input, , create date/time object:

5 o'clock 5 p.m. 5 a.m. 5 530 530 a.m. 530 p.m. tuesday @ [insert above string here] 30th @ [same above] may 12th @ [same above] today @ [same above] tomorrow @ [same above] 

any string doesn't contain day/date can assumed being today, , time not have am/pm designation can assumed occuring between 9am , 8:59pm. realized mess becoming after writing portion of code:

private void createevent(string phrase) {     int hour;     int day = 0;     string dayofweek = "";      if (phrase.contains("o'clock")) {         hour = integer.parseint(phrase.substring(phrase.indexof("o'clock")-3, phrase.indexof("o'clock")-1).trim());         out.write(""+hour);     }      if (phrase.contains("tomorrow"))         day = (calendar.day_of_week % 7)+1;      if (phrase.contains("sunday") || day == 1) {         dayofweek = "sunday"; day = 1; }     else if (phrase.contains("monday") || day == 2) {         dayofweek = "monday"; day = 2; }     else if (phrase.contains("tuesday") || day == 3) {         dayofweek = "tuesday"; day = 3; }     else if (phrase.contains("wednesday") || day == 4) {         dayofweek = "wednesday"; day = 4; }     else if (phrase.contains("thursday") || day == 5) {         dayofweek = "thursday"; day = 5; }     else if (phrase.contains("friday") || day == 6) {         dayofweek = "friday"; day = 6; }     else if (phrase.contains("saturday") || day == 7) {         dayofweek = "saturday"; day = 7; }     else {         dayofweek = "today"; day = 0; }      } 

can provide direction?

you use dateformat

string = new date().tostring(); simpledateformat format = new simpledateformat("eee mmm dd hh:mm:ss zzz yyyy"); date date = format.parse(now); 

maybe build few format in advance, compare parsed dates.

check tutorial here: http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -