All I want to do is display a nicely formatted date. You know, something like, “Mon 20-Aug-01”, or perhaps “8/21/01”, or maybe even “Mon Aug 20 15:00:00 PDT 2001”. This was easy enough using the deprecated Date object definition, but I was advised at my Java class to avoid using Date, but instead to use GregorianCalendar. Besides, I found that using Date objects conflicts with the date definition in the sql package, which I also need to use.
Java provides a toString overload on GregorianCalendar, to be used for debugging purposes. AFAI can tell the year, month, day, hour and minute members are being set correctly when I create the objects, but if I try to display or print out those members they come out completely wrong.
How friggin’ hard is this supposed to be? I’ve learned lots of languages but never seen anything that should be so simple turn out to be so difficult. What am I missing?
A) Sun f*cked up with the java.util.Date vs. java.sql.Date. Just remember that you can always use java.sql.Dates where you need a java.util.Date and you can always get the former from the latter with a
new java.util.Date(mySqlDate.getTime());
B)
Calendar cal = new GregorianCalendar();
cal.set(Calendar.MONTH, Calendar.AUGUST);
cal.set(Calendar.DATE, 20);
cal.set(Calendar.YEAR, 2001);
C)
String formattedDate = new SimpleDateFormat("M/d/yy").format(cal.getTime());
D) Please don’t ask me why Calendar.getTime() returns a Date
E) Please, please don’t ask me why, by contrast, Date.getTime() returns a long
Oh yeah, perl is so much better. Months run from 0-11 but days run from 1-31. While years are specified as (actual year - 1900), so 2001 = 101. SO intuitive!
Thanks, everyone. I’m glad to know it’s not me, after all, but everyone who things the date-handing in Java is a tad klujie.
Said instructor sent me an email yesterday with the following sample code. I haven’t had a chance to try it yet, but it looks like it should solve the problem:
public class DateTest
{
public static void main(String s)
{
DateFormat f = DateFormat.getDateInstance();
It’s not very hard at all. Sun removed all formatting out of java.util.Date because they provided a powerful formatting library in java.text. The following illustrated formatting the same Date object eight different ways using two different instances of java.text.DateFormat:
import java.text.DateFormat;
import java.util.Date;
public class DateTest
{
public static void main(String[] args)
{
int[] styles = new int[]
{
DateFormat.SHORT,
DateFormat.MEDIUM,
DateFormat.LONG,
DateFormat.FULL
};
Date date = new Date();
System.out.println("Formatted using DateFormat.getDateInstance()");
for (int i = 0; i < styles.length; i++)
{
System.out.println(" " + DateFormat.getDateInstance(i).format(date));
}
System.out.println();
System.out.println("Formatted using DateFormat.getTimeInstance()");
for (int i = 0; i < styles.length; i++)
{
System.out.println(" " + DateFormat.getTimeInstance(i).format(date));
}
}
}
You can also use DateFormat.getDateTimeInstance(…) to combine different styles of date and time formatting. Plus, of course, you can specify a Locale. And, if you are really particular, you can use meara’s advice and use SimpleDateFormat to format a date so only you can read it. Possibilities are endless!
D) It returns a Date because Date is a standard (one might say object oriented) way of representing a date and a time in Java. That’s what you should always use unless you are really performance oriented, in which case see (E)
E) long is what’s returned by System.currentTimeMillis(), which is faster than ‘new Date()’ and is often used in performance oriented systems. There has to be a way for “long” and “Date” dates to coexist (for comparison or whatever), so that’s why Date.getTime() was provided. The naming is a little strange, but you get used to it.