Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: handle time-expressions

I am using a Java backend to store different events. Up to now Date is used to store date and time specific information. It would be nice to allow "more natural" time definitions like "afternoon", "morning" or "soon".

Are there any best practices to handle these flexible/not well specified but often used time specifiers? Are there some libraries to transform them into Date objects (and vice versa)?

like image 558
NaN Avatar asked Feb 26 '26 12:02

NaN


1 Answers

How about doing that by hand. Just create a wrapper class and make an enum with values AfterNoon, Morning, ... And provide a constructor that takes a Date object and code the logic as you want.

Class DateWrapper {
      private Date date;
      private DayTime dayTime;

      public DateWrapper (Date date){
         ...
       }


       public enum DayTime {
         Morning, AfterNoon, ...;
        }
}
like image 128
javaHunter Avatar answered Feb 28 '26 01:02

javaHunter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!