Date intervall parsing

S

Steve

Hi,

does anybody know, where I can find a class
converting Strings like 11./12.05.2005 or 13.04.-16.08.2006
into two separate Date objects by given a pattern?

Is there an appropriate pattern in SimpleDateFormat?


Regards
Stephan Melchior
 
S

shakah

Steve said:
Hi,

does anybody know, where I can find a class
converting Strings like 11./12.05.2005 or 13.04.-16.08.2006
into two separate Date objects by given a pattern?

Is there an appropriate pattern in SimpleDateFormat?


Regards
Stephan Melchior

It sounds like SimpleDateFormat will work for you, e.g.:

jc@soyuz:~/tmp$ cat dptest.java
public class dptest {
public static void main(String [] asArgs) {
System.out.println("parsing pattern: '" + asArgs[0] + "'") ;
java.text.DateFormat df = new java.text.SimpleDateFormat(asArgs[0])
;
System.out.println("now is: '" + df.format(new java.util.Date()) +
"'") ;
for(int nArg=1; nArg<asArgs.length; ++nArg) {
try {
System.out.println("'" + asArgs[nArg]
+ "' => " + df.parse(asArgs[nArg])) ;
}
catch(Exception x) {
System.out.println("'" + asArgs[nArg]
+ "' => caught: " + x) ;
}
}
}
}

jc@soyuz:~/tmp$ javac dptest.java

jc@soyuz:~/tmp$ java dptest 'HH.mm.-dd.MM.yyyy' '13.04.-16.08.2006'
parsing pattern: 'HH.mm.-dd.MM.yyyy'
now is: '10.47.-18.07.2005'
'13.04.-16.08.2006' => Wed Aug 16 13:04:00 EDT 2006
 
S

Steve

Sorry if I have not explained the problem properly.

The String '13.04.-16.08.2006' means the interval from
13.04.2006 until 16.08.2006.

I'm looking for a method like

Date getDate(String str, boolean start){
//mystic code
}
with
getDate("13.04.-16.08.2006", true)
=> 13.04.2006 (as Date)
resp.
getDate("13.04.-16.08.2006", false)
=> 16.04.2006 (as Date)

Greetz
Stephan
 
S

shakah

Steve said:
Sorry if I have not explained the problem properly.

The String '13.04.-16.08.2006' means the interval from
13.04.2006 until 16.08.2006.

I'm looking for a method like

Date getDate(String str, boolean start){
//mystic code
}
with
getDate("13.04.-16.08.2006", true)
=> 13.04.2006 (as Date)
resp.
getDate("13.04.-16.08.2006", false)
=> 16.04.2006 (as Date)

Greetz
Stephan

A bit of a hack, and certainly not pretty, but you could use two
different SimpleDateFormat strings:

jc@soyuz:~/tmp$ java dptest 'dd.MM.-SS.SS.yyyy' '13.04.-16.08.2006'
parsing pattern: 'dd.MM.-SS.SS.yyyy'
now is: '18.07.-223.223.2005'
'13.04.-16.08.2006' => Thu Apr 13 00:00:00 EDT 2006

jc@soyuz:~/tmp$ java dptest 'SS.SS.-dd.MM.yyyy' '13.04.-16.08.2006'
parsing pattern: 'SS.SS.-dd.MM.yyyy'
now is: '863.863.-18.07.2005'
'13.04.-16.08.2006' => Wed Aug 16 00:00:00 EDT 2006
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top