I am designing a system for bookings and need to represent dates only for the year 2015, in the format "dd, mmm, 2015" (e.g. "05 jan 2015"). Im slightly confused about how to do this, it looks like Date supported something like this but has now been depreciated? I'm also confused by the gregorian calendar classes is GregorianCalender(2015, 01, 05) a representation of a date in 2015 or another object entirely?
-
1What do you mean by represent dates, is it having a date object and you want to display it as a String with the format "dd mmm 2015" ? – Mooolo Apr 07 '15 at 01:48
-
Its for a booking system for rooms, so i want to represent the dates a room is booked and check if a room is not booked etcetera. but I also want dates to be valid for 2015. – Massin Apr 07 '15 at 01:52
-
2The LocalDate class in Java 8 is a very good option. If you have a lower version of Java, then have a look at the JodaTime library. http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html – Erwin Bolwidt Apr 07 '15 at 01:52
-
1If you are using Java 8, then use the new Time API, if not, use JodaTime. Java `Date` doesn't have a concept of format, it is just the number of milliseconds since the Unix epoch. Use a `SimpleDateFormat` to actually format the date in what ever format you actually want... – MadProgrammer Apr 07 '15 at 01:53
-
Use a date module that allows you to format your date. Then choose a YYYY/MM/DD format because then you can convert them to integers and just use conditional operators to determine if one date is equal to, less than, or greater than another date etc. – Ogen Apr 07 '15 at 02:01
-
So would you do something like this SimpleDateFormat("yyyy.mmm.dd") LocalDate.of(2015, jan, 05) ? – Massin Apr 07 '15 at 02:04
4 Answers
java.time.LocalDate
If you're using Java 8 or later, you should use java.time.LocalDate
class.
To parse and format this date, you need to use java.time.format.DateTimeFormatter
class.
Usage example:
LocalDate date = LocalDate.of(2015, Month.JANUARY, 5);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd, MMM, yyyy", Locale.ENGLISH);
System.out.println(formatter.format(date)); // prints "05, Jan, 2015"
date = LocalDate.parse("06, Jan, 2015", formatter);
System.out.println(date.getDayOfMonth()); // prints "6"
If you're using Java 7 or earlier, you should use java.util.Date
class to represent a date, java.util.GregorianCalendar
to create a date object from fields or to retrieve date components and java.text.SimpleDateFormat
to parse and format. Usage example:
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.set(2015, Calendar.JANUARY, 5);
Date date = gregorianCalendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd, MMM, yyyy", Locale.ENGLISH);
System.out.println(simpleDateFormat.format(date)); // prints "05, Jan, 2015"
date = simpleDateFormat.parse("06, Jan, 2015");
gregorianCalendar.setTime(date);
System.out.println(gregorianCalendar.get(Calendar.DAY_OF_MONTH)); // prints "6"

- 303,325
- 100
- 852
- 1,154

- 11,148
- 9
- 49
- 63
-
1The first part is wise, using *java.time* classes. As for the second part, about Java 7 & Java 6, I would instead suggest that people add the back-port of most of *java.time* functionality found in the [*ThreeTen-Backport*](https://www.threeten.org/threetenbp/) project. The API in nearly identical to that of *java.time*. So no need to learn two different frameworks. More importantly, those legacy classes (`Date`, `Calendar`, `GregorianCalendar`) are *really* bad classes, atrocious design, very confusing, and frustrating to utilize. – Basil Bourque May 12 '20 at 00:24
Java 8 Date and Time API provide you lot of flexibility on Date and Time usage. Find more about Java 8 Date and Time

- 522
- 1
- 5
- 10
I'm unsure of where you saw the deprecation, but I would still use a Date and DateFormat object.
DateFormat format = new SimpleDateFormat("dd MM");
String output;
output = format.format(new Date()) + " 2015";
System.out.println(output);
Output:
06 04 2015
If you wanted to remove the 0s, change the code to this:
DateFormat format = new SimpleDateFormat("dd MM");
String output;
output = format.format(new Date());
output = output.replaceAll("0", "") + " 2015";
System.out.println(output);
Output:
6 4 2015
For more information on SimpleDateFormat, here's the javadoc: SimpleDateFormat Reference

- 522
- 1
- 5
- 10

- 839
- 2
- 9
- 15
-
1Just use `"d M"` or `"d M yyyy"` if you don't want the zeroes. Your code goes badly wrong on the 10th, 20th and 30th of each month, and also all of October. Aside from the fact that it only works in 2015, of course. – Dawood ibn Kareem Apr 07 '15 at 03:48
-
You got me there. And yes, it only works in 2015; that's what the question asked for. – maxbfuer Apr 07 '15 at 05:44
-
*I'm unsure of where you saw the deprecation* The questioner may have been looking at one of the deprecated constructors. In any case `Date` is poorly and confusingly designed, and despite the name it does not represent a date. It is also obsolete, outdated, outmoded. Not officially deprecated (yet), we will benefit from treating as though it is. – Ole V.V. May 12 '20 at 04:59
-
@OleV.V. it's still used in many APIs like `java.security`, `java.sql`, `java.util.Timer` and many other classes. Deprecating them all would require quite a work. It should not be used in new code, but for some existing code it's unavoidable. Thankfully it's trivial to convert between `java.time.*` and `java.util.Date`. – vbezhenar May 13 '20 at 22:00
-
1@vbezhenar Generally correct. They could easily deprecate the uses in `java.sql`, though. Since JDBC 4.2 [we can pass java.time objects to and from SQL](https://stackoverflow.com/a/54907501/5772882). – Ole V.V. May 14 '20 at 05:55
This will get you today's date as an int
with the year month then day. You can use this int to compare dates. I.e., dateInt_1 < dateInt_2
would mean that date1
came before date2
. dateInt_1 == dateInt_2
would meant that the two dates are the same. etc.
int dateInt = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));

- 6,499
- 7
- 58
- 124