Wednesday, October 20, 2010

Troubling Dates

One of the most common challenges you will have to handle when you’re programming, and even in life, is handling or managing dates. In programming, there are a lot of date formatting, parsing, and comparison that you might need to do for your program. Each language, however, has a different approach in doing these activities. For this entry I will be providing a few methods for doing these activities.

Formatting
The is probably the most common date activity that you will need to learn how to handle and Java provides a way to format your java.util.Date objects and convert them to String. The simplest way to format your date to string is thru the use of java.text.SimpleDateFormat class. To achieve this you need to know first the patterns that you can use.
Letter Date/Time Component Presentation Example
G
Era designator
Text
AD
y
Year
Year
1996; 96
M
Month in year
Month
July; Jul; 07
w
Week in year
Number
27
W
Week in month
Number
2
D
Day in year
Number
189
d
Day in month
Number
10
F
Day of week in month
Number
2
E
Day in week
Text
Tuesday; Tue
a
Am/pm marker
Text
PM
H
Hour in day (0-23)
Number
0
k
Hour in day (1-24)
Number
24
K
Hour in am/pm (0-11)
Number
0
h
Hour in am/pm (1-12)
Number
12
m
Minute in hour
Number
30
s
Second in minute
Number
55
S
Millisecond
Number
978
**For a complete reference visit: java.text.SimpleDateFormat api

The idea here is to combine the letters to create a pattern that will serve as your template for formatting your Date objects. For example, if I want to format from a date object the day Kurt Cobain died into this string: "April 8, 1994", then I should use this pattern: "MMMM d, yyyy". Try exploring different patterns and see the result. You may want to use this method:
//Date is java.util.Date
public static String format(String pattern, Date date) {
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    return formatter.format(date);
}
Parsing
Parsing is just the reverse of formatting. Given a String, you simply would want that String to become a java.util.Date. Doing this might throw a ParseException if the string cannot be parsed. You should be able to handle this Exception. The following method will allow you to do a parsing activity:
public static Date parse(String pattern, String dateString) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    return formatter.parse(dateString);
}
Comparison
The first two activities are pretty easy. Date comparison, I would say, is probably the trickiest activity to work on. There is an easy way to compare dates using the java.util.Calendar singleton class but there are certain scenarios where those methods may not be useful. For example, you simply want to compare if two dates are equal but not necessarily check if the times are equal as well; calendar.equals doesn't really help in that matter. For other things such as checking if a date is before/after another date you may use the calendar.before and calendar.after methods. The following method will be able to answer that problem:
public static boolean isDateEqual(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);
    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) 
        && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) 
        && cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE);
}
**For a complete reference visit: java.util.Calendar api

What's the date now?
Another thing you need to learn how to do is how to get the current date and time. It's not so easy to get the current date and time in Java compared to other languages:
public static Date getToday() {
    Date now = Calendar.getInstance().getTime();
    return now;
}
There are different ways to handle these activities and the solutions I have provided are very basic. For date comparison you might want to explore the Comparator and Comparable interfaces. Thank you for taking time to read. I hope you learned something.

No comments: