Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to represent a time period in Java?

Tags:

java

date

What's the best way to represent a time period in Java? May I use 2 Date objects or is there a better way?

I.e. all April, the hole 3rd week of July, from January to March, etc.

like image 686
Joaquín L. Robles Avatar asked May 27 '11 13:05

Joaquín L. Robles


People also ask

How do you represent a period in Java?

The Period class uses the units year, month and day to represent a period of time. We can obtain a Period object as the difference between two dates by using the between() method: LocalDate startDate = LocalDate. of(2015, 2, 20); LocalDate endDate = LocalDate.

Which datatype is used for time in Java?

TIME Type. The time data type. The format is yyyy- MM -dd hh:mm:ss, with both the date and time parts maintained. Mapped to java.

How do you represent a day in Java?

Format dateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy"); Above, the “EEE” is set to display the name of the day i.e. Monday, Tuesday, Wednesday, etc.


1 Answers

I see the answers are quite old.

As of Java 8, we can use the native Java.Time which is partially based on Joda Time and makes the manipulations on dates very easy.

Take a look at Period, Duration and LocalDate on

  • https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
  • https://dzone.com/articles/deeper-look-java-8-date-and
like image 103
Orkun Ozen Avatar answered Sep 19 '22 03:09

Orkun Ozen