← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15012: support date formats in calendar, wip

 

------------------------------------------------------------
revno: 15012
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-04-27 14:32:17 +0545
message:
  support date formats in calendar, wip
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java	2014-04-27 07:19:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java	2014-04-27 08:47:17 +0000
@@ -30,6 +30,8 @@
 
 import org.joda.time.DateTime;
 import org.joda.time.chrono.ISOChronology;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -86,6 +88,33 @@
         "weekday.short.sunday"
     };
 
+    protected static final String DEFAULT_ISO8601_DATE_FORMAT = "yyyy-MM-dd";
+
+    @Override
+    public String defaultDateFormat()
+    {
+        return DEFAULT_ISO8601_DATE_FORMAT;
+    }
+
+    @Override
+    public String formattedDate( DateUnit dateUnit )
+    {
+        DateTime dateTime = dateUnit.toDateTime();
+        DateTimeFormatter format = DateTimeFormat.forPattern( defaultDateFormat() );
+
+        return format.print( dateTime );
+    }
+
+    @Override
+    public String formattedIsoDate( DateUnit dateUnit )
+    {
+        dateUnit = toIso( dateUnit );
+        DateTime dateTime = dateUnit.toDateTime();
+        DateTimeFormatter format = DateTimeFormat.forPattern( defaultDateFormat() );
+
+        return format.print( dateTime );
+    }
+
     @Override
     public DateUnit toIso( int year, int month, int day )
     {

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-04-27 07:19:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java	2014-04-27 08:47:17 +0000
@@ -43,6 +43,26 @@
     String name();
 
     /**
+     * Default date format for this calendar
+     * @return Default date format
+     */
+    String defaultDateFormat();
+
+    /**
+     * Formats dateUnit using defaultDateFormat
+     * @param dateUnit DateUnit representing local year, month, day
+     * @return Default date format
+     */
+    String formattedDate( DateUnit dateUnit );
+
+    /**
+     * Formats dateUnit using defaultDateFormat and ISO 8601
+     * @param dateUnit DateUnit representing local year, month, day
+     * @return Default date format
+     */
+    String formattedIsoDate( DateUnit dateUnit );
+
+    /**
      * Convert local calendar to an ISO 8601 DateUnit.
      * @param year  Local year
      * @param month Local month

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java	2014-04-26 06:39:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/DateUnit.java	2014-04-27 08:47:17 +0000
@@ -33,6 +33,8 @@
 import org.joda.time.chrono.ISOChronology;
 
 import javax.validation.constraints.NotNull;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 /**
  * Class representing a specific calendar date.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java	2014-04-27 07:19:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/impl/NepaliCalendar.java	2014-04-27 08:47:17 +0000
@@ -47,9 +47,11 @@
 @Component
 public class NepaliCalendar extends AbstractCalendar
 {
-    private final DateUnit startNepal = new DateUnit( 2000, 1, 1, java.util.Calendar.WEDNESDAY );
-
-    private final DateUnit startIso = new DateUnit( 1943, 4, 14, java.util.Calendar.WEDNESDAY );
+    private static final DateUnit startNepal = new DateUnit( 2000, 1, 1, java.util.Calendar.WEDNESDAY );
+
+    private static final DateUnit startIso = new DateUnit( 1943, 4, 14, java.util.Calendar.WEDNESDAY );
+
+    private static final String DEFAULT_NEPALI_DATE_FORMAT = "dd-MM-yyyy";
 
     private static final Calendar self = new NepaliCalendar();
 
@@ -59,6 +61,25 @@
     }
 
     @Override
+    public String name()
+    {
+        return "nepali";
+    }
+
+    @Override
+    public String defaultDateFormat()
+    {
+        return DEFAULT_NEPALI_DATE_FORMAT;
+    }
+
+    // TODO Add proper local date format for Nepali, can't use default.. since the month numbers are outside the valid ISO 8601 range
+    @Override
+    public String formattedDate( DateUnit dateUnit )
+    {
+        return super.formattedDate( dateUnit );
+    }
+
+    @Override
     public DateUnit toIso( DateUnit dateUnit )
     {
         DateTime dateTime = startIso.toDateTime();
@@ -221,12 +242,6 @@
     }
 
     @Override
-    public String name()
-    {
-        return "nepali";
-    }
-
-    @Override
     public String nameOfMonth( int month )
     {
         if ( month > DEFAULT_I18N_MONTH_NAMES.length || month <= 0 )