This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e6564b5 EMPIREDB-431 DateUtils: added some date field functions for 
convenience
2e6564b5 is described below

commit 2e6564b5010a3523e63c8139e5bd03338cd56ac7
Author: Rainer Döbele <[email protected]>
AuthorDate: Mon Feb 17 13:47:18 2025 +0100

    EMPIREDB-431
    DateUtils: added some date field functions for convenience
---
 .../java/org/apache/empire/commons/DateUtils.java  | 54 ++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java 
b/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
index ae646e9b..c29cba96 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
@@ -25,8 +25,10 @@ import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
+import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.format.FormatStyle;
+import java.time.temporal.ChronoField;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Locale;
@@ -205,6 +207,58 @@ public class DateUtils
         return (int)TimeUnit.DAYS.convert(diffInMillies, 
TimeUnit.MILLISECONDS);
     }
     
+    // ------- date-fields -----
+    
+    public static int getYear()
+    {
+        return Calendar.getInstance().get(Calendar.YEAR);    
+    }
+    
+    public static int getYear(Date date)
+    {
+        // Get the year from the ZonedDateTime
+        ZonedDateTime dateTime = 
date.toInstant().atZone(ZoneId.systemDefault()); 
+        return dateTime.get(ChronoField.YEAR);         
+    }
+    
+    public static int getMonth()
+    {
+        return Calendar.getInstance().get(Calendar.MONTH)+1;    
+    }
+    
+    public static int getMonth(Date date)
+    {
+        // Get the year from the ZonedDateTime
+        ZonedDateTime dateTime = 
date.toInstant().atZone(ZoneId.systemDefault()); 
+        return dateTime.get(ChronoField.MONTH_OF_YEAR);         
+    }
+    
+    public static int getDay()
+    {
+        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);    
+    }
+    
+    public static int getDay(Date date)
+    {
+        // Get the year from the ZonedDateTime
+        ZonedDateTime dateTime = 
date.toInstant().atZone(ZoneId.systemDefault()); 
+        return dateTime.get(ChronoField.DAY_OF_MONTH);         
+    }
+    
+    public static int getDayOfWeek()
+    {
+        // Monday (1) to Sunday (7)
+        int dow = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
+        return (dow<=0 ? 7 : dow);    
+    }
+    
+    public static int getDayOfWeek(Date date)
+    {
+        // Get the year from the ZonedDateTime
+        ZonedDateTime dateTime = 
date.toInstant().atZone(ZoneId.systemDefault()); 
+        return dateTime.get(ChronoField.DAY_OF_WEEK);         
+    }
+    
     // ------- parsing functions -----
 
     public static Date parseDate(String sDate, Locale locale)

Reply via email to