How to parse an ambiguous string in Date?

I am trying to figure out a “simple” way to parse a string into a Date object.

The string can be yyyyMMdd, yyyyMMddHHmm or yyyyMMddHHmmSS.

I am currently looking at the length of a string and create a DateParser depending on the length. Is there a more elegant way to do this?

+4
source share
5 answers

Or you can put your string in zeros:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSS") { @Override public Date parse(String s) throws ParseException { return super.parse((s + "000000").substring(0, 14)); } }; System.out.println(sdf.format(sdf.parse("20110711182405"))); System.out.println(sdf.format(sdf.parse("201107111824"))); System.out.println(sdf.format(sdf.parse("20110711"))); 
+6
source

I would do as you would by looking at the length of the string and creating the corresponding SimpleDateFormat instance.

 SimpleDateFormat getFormatFor( String dateString ){ if ( dateString.length() == 8 ) return new SimpleDateFormat("yyyyMMdd"); if ( dateString.length() == 14 ) return new SimpleDateFormat("yyyyMMddHHmmss"); // you got a bad input... } 

NB they are not thread safe, so you should create a new one every time.

+4
source

I would use the SimpleDateFormat class and populate a format template based on the length of the string. This will work fine if one day you don’t have lines of the same length.

Using examples from your question:

Formatting July 11, 2011:

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); Date parsedDate = dateFormat.parse("20110711"); 

Formatting July 11, 2011 1340 hours:

 dateFormat = new SimpleDateFormat("yyyyMMddHHmm"); parsedDate = dateFormat.parse("201107111340"); 

Formatting July 11, 2011 1340 hours 10 seconds:
(NB. Small s for seconds, capital S for milliseconds!)

 dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); parsedDate = dateFormat.parse("20110711134010"); 

See the hyperlink for a complete list of format template letters.

+2
source

You could still use “specialized” parsers (as you expected) and bind them: For example, you still have DateHourMinSecParser (for yyyyMMddHHmmSS ), DateHourMinParser (for yyyyMMddHHmm ) and DateParser (for yyyyMMdd ) all of which same interface:

 public interface GenericDateParser { Date parseDate(String input) throws IllegalArgumentException; } 

eg.

 public class DateHourMinSecParser implements GenericDateParser { ... public Date parseDate(String input) throws IllegalArgumentException { ... } } 

but each of these classes would actually take another GenericDateParser parameter - the idea was that each parser would first try to parse the date if the parsing (or some internal checks, such as the length of the string), then pass it to the next parser in the chain until there are no more parsers in the chain (in this case, it will throw an exception, or one of the members in the chain will return a value):

 public class DateHourMinSecParser implements GenericDateParser { private GenericDateParser chained; public DateHourMinSecParser(GenericDateParser chained) { this.chained = chained; } public Date parseDate(String input) throws IllegalArgumentException { if( !internalChecks() ) { //chain it up if( chained == null ) throw new IllegalArgumentException( "Don't know how to parse " + input); } //internal checks passed so try to parse it and return a Date or throw exception ... } } 

and you initialize them:

 GenericDateParser p = new DateHourMinSecParser( new DateHourMinParser(new DateParser(null)) ); 

and then just use the top level:

 Date d = p.parse( '20110126' ); 
0
source

You can use DateFormatter to parse a date from a string.

 import java.util.*; import java.text.*; public class StringToDate { public static void main(String[] args) { try { String str_date="11-June-07"; DateFormat formatter ; Date date ; formatter = new SimpleDateFormat("yyyy-MM-dd"); date = (Date)formatter.parse(str_date); } catch (ParseException e) { System.out.println("Exception :"+e); } } } 

You can change the template, but want to reflect your needs.

-1
source

All Articles