I have the following code generating a random date, but I don't know how to display it in the form of DD-MON-YY
(for example 12-NOV-12
), which happens to be the SQL standard format for dates (i.e. in the end I plan on passing this data with JDBC into an oracle database).
GregorianCalendar gc = new GregorianCalendar();
gc.set(gc.YEAR, randBetween(1900, 2015)); //randBetween creates a random number between the two
gc.set(gc.DAY_OF_YEAR, randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR)));
//Mysterious formatting needed here
String s = //date would go here in the DD-MON-YY format
Anyway, point being I have tried many ways to format it and the closest I've got it to has been DD-MM-YY
(for example 12-12-12
). I was wondering if anyone knew of a short/easy way to achieve my goal without resorting to the primitive long way (if/switch-case statements).
Use
SimpleDateFormat
:This is the part in the javadoc you were looking for: