Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams
this week in the
Agile and Other Processes
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Devaka Cooray
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Jeanne Boyarsky
Tim Cooke
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Bartenders:
Forum:
Beginning Java
dates
David Jon
Greenhorn
Posts: 17
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
if i have 2 dates stored in Strings in the form d/mm/yyyy does anyone know of a neat way I can compare them to see if the first one is before the second one.
Cheers,
Jon.
Art Metzer
Ranch Hand
Posts: 241
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, David.
The trick lies in the use of the java.text.SimpleDateFormat class:
import java.text.*; import java.util.*; public class WhichDateBefore { public static void main( String[] args ) { Date start = null; // these Dates will be populated Date end = null; // with values from the strings SimpleDateFormat sdf = new SimpleDateFormat( "d/MM/yyyy" ); String now = "27/04/2001"; String then = "31/12/1999"; try { start = sdf.parse( then ); end = sdf.parse( now ); } catch ( ParseException pe ) { System.out.println( pe.toString() ); } System.out.println( start + " before " + end + "? " + start.before( end ) ); } }
This code returns
Fri Dec 31 00:00:00 CST 1999 before Fri Apr 27 00:00:00 CDT 2001? true
Good luck,
Art
today's feeble attempt to support the empire
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
comparision between two dates
Get date List between two dates in mysql
comparing dates
Date comparision in java
Date difference, with the table containing null values for date
More...