Massin
Massin

Reputation: 117

Best way to represent dates in java

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?

Upvotes: 5

Views: 17784

Answers (4)

vbezhenar
vbezhenar

Reputation: 12316

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"

Upvotes: 6

Ogen
Ogen

Reputation: 6709

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()));

Upvotes: 0

maxbfuer
maxbfuer

Reputation: 859

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

Upvotes: 0

Annamalai Thangaraj
Annamalai Thangaraj

Reputation: 532

Java 8 Date and Time API provide you lot of flexibility on Date and Time usage. Find more about Java 8 Date and Time

Upvotes: 1

Related Questions