27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

String, text, numbers, I/O II part<br />

Receive our newsletter with new tips! Almost 6,000 subscribers (by June 2001) can not be wrong!<br />

They read our tips every week! To subscribe to The <strong>Java</strong> FAQ Daily send empty e-mail to:<br />

javafaq-tips-subscribe@topica.com or visit at:<br />

http://www.topica.com/lists/javafaq-tips/<br />

String, text, numbers, I/O II part<br />

Q: How do I parse it to date and make a date difference?? How many days in<br />

between the String from and to?<br />

I am always confusing with date and time.<br />

I have 2 String, from and to<br />

String from = "01.01.2001" //dd.MM.yyyy<br />

String to = "01.03.2001" //dd.MM.yyyy<br />

How do I parse it to date and make a date difference?? How many days in between<br />

the String from and to?<br />

Answer:<br />

import java.text.SimpleDateFormat;<br />

import java.util.Date;<br />

public class Tmp {<br />

public static void main( String argv[] ) throws Exception {<br />

long DAY = 24L * 60L * 60L * 1000L;<br />

SimpleDateFormat df = new SimpleDateFormat( "MM.dd.yyyy" );<br />

Date d1 = df.parse( "01.01.2001" );<br />

Date d2 = df.parse( "01.03.2001" );<br />

System.out.println( "The number days between:" );<br />

System.out.println( d1 );<br />

System.out.println( "and:" );<br />

System.out.println( d2 );<br />

System.out.println( "is: " + (( d2.getTime() - d1.getTime() ) / DAY ));<br />

}<br />

}<br />

file:///F|/350_t/350_tips/stings_text__date_numbers_io-II.htm (1 of 7) [2002-02-27 21:19:21]<br />

Visit us here and you will find<br />

much more tips!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!