Mercurial > hg > openmind
view src/main/java/org/mpi/openmind/repository/utils/ismi/ISMIDate.java @ 127:3e772f7f43e0 default tip
ismi-date with long month names in xml dump.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 11 May 2023 18:15:45 +0200 |
parents | 8d79021099a4 |
children |
line wrap: on
line source
package org.mpi.openmind.repository.utils.ismi; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import org.joda.time.DateTime; import org.joda.time.chrono.GregorianChronology; import org.joda.time.chrono.IslamicChronology; import org.joda.time.chrono.JulianChronology; import org.json.JSONException; import org.json.JSONObject; public class ISMIDate implements Serializable{ private static final long serialVersionUID = -6470414082851873885L; public static String AMBIGUITY = "2"; static Map<Integer, String> gregorianMonths = new HashMap<Integer, String>(12); static Map<Integer, String> islamicMonths = new HashMap<Integer, String>(12); static Map<Integer, String> julianMonths = new HashMap<Integer, String>(12); static{ gregorianMonths.put(1, "January (1)"); gregorianMonths.put(2, "February (2)"); gregorianMonths.put(3, "March (3)"); gregorianMonths.put(4, "April (4)"); gregorianMonths.put(5, "May (5)"); gregorianMonths.put(6, "June (6)"); gregorianMonths.put(7, "July (7)"); gregorianMonths.put(8, "August (8)"); gregorianMonths.put(9, "September (9)"); gregorianMonths.put(10, "October (10)"); gregorianMonths.put(11, "November (11)"); gregorianMonths.put(12, "December (12)"); islamicMonths.put(1, hex("Muḥarram") + " (1)"); islamicMonths.put(2, hex("Ṣafar") + " (2)"); islamicMonths.put(3, hex("Rabīʿ I") + " (3)"); islamicMonths.put(4, hex("Rabīʿ II") + " (4)"); islamicMonths.put(5, hex("Jumādỳ I") + " (5)"); islamicMonths.put(6, hex("Jumādỳ II") + " (6)"); islamicMonths.put(7, "Rajab (7)"); islamicMonths.put(8, hex("Šaʿbān") + " (8)"); islamicMonths.put(9, hex("Ramaḍān") + " (9)"); islamicMonths.put(10, hex("Šawwāl") + " (10)"); islamicMonths.put(11, hex("Ḏu al-Qaʿdaẗ") + " (11)"); islamicMonths.put(12, hex("Ḏu al-Ḥijjaẗ") + " (12)"); julianMonths.put(1, "Ianuarius (1)"); julianMonths.put(2, "Februarius (2)"); julianMonths.put(3, "Martius (3)"); julianMonths.put(4, "Aprilis (4)"); julianMonths.put(5, "Maius (5)"); julianMonths.put(6, "Iunius (6)"); julianMonths.put(7, "Quintilis (Iulius) (7)"); julianMonths.put(8, "Sextilis (Augustus) (8)"); julianMonths.put(9, "September (9)"); julianMonths.put(10, "October (10)"); julianMonths.put(11, "November (11)"); julianMonths.put(12, "December (12)"); } public static String hex(String s){ Character c = 0x1e25; s = s.replace("ḥ", c + ""); c = 0x1e62; s = s.replace("Ṣ", c + ""); c = 0x12b; s = s.replace("ī", c + ""); c = 0x2bf; s = s.replace("ʿ", c + ""); c = 0x101; s = s.replace("ā", c + ""); c = 0x1ef3; s = s.replace("ỳ", c + ""); c = 0x160; s = s.replace("Š", c + ""); c = 0x1e0d; s = s.replace("ḍ", c + ""); c = 0x1e0e; s = s.replace("Ḏ", c + ""); c = 0x1e24; s = s.replace("Ḥ", c + ""); c = 0x1e97; s = s.replace("ẗ", c + ""); return s; } protected boolean isDayInWeek(Integer day){ if(day == null) return false; if(day >= 1 && day <= 7){ return true; } return false; } private Integer dayOfMonth; private Integer dayOfWeek; private Integer dayOfYear; private Integer month; private Integer year; private Integer century; private Integer ambiguity; public ISMIDate(){} public ISMIDate(DateTime dateTime){ this.setTime(dateTime); this.ambiguity = 0; } /* public Date(Integer year, Integer month, Integer dayOfMonth){ this.year = year; this.month = month; this.dayOfMonth = dayOfMonth; }*/ public ISMIDate(JSONObject json) { try{ dayOfMonth = json.getInt("dayOfMonth"); month = json.getInt("month"); year = json.getInt("year"); //century = json.getInt("century"); if(json.has("century")){ century = json.getInt("century"); } if(json.has("dayOfWeek")){ dayOfWeek = json.getInt("dayOfWeek"); } if(json.has("dayOfYear")){ dayOfYear = json.getInt("dayOfYear"); } if(json.has("ambiguity")){ ambiguity = json.getInt("ambiguity"); } }catch (Exception e) { e.printStackTrace(); } } public void setTime(DateTime dateTime){ //TODO is gregorian chronology this.dayOfMonth = dateTime.getDayOfMonth(); this.dayOfYear = dateTime.getDayOfYear(); //this.dayOfWeek = dateTime.getDayOfWeek(); this.month = dateTime.getMonthOfYear(); this.year = dateTime.getYear(); this.century = dateTime.getCenturyOfEra(); } public JSONObject toJSON(){ JSONObject json = new JSONObject(); try { json.put("dayOfMonth", dayOfMonth); json.put("dayOfYear", dayOfYear); json.put("month", month); json.put("year", year); json.put("century", century); json.put("dayOfWeek", dayOfWeek); json.put("ambiguity", ambiguity); } catch (JSONException e) { e.printStackTrace(); } return json; } public String toString(){ if(year == null || month == null || dayOfMonth == null) return null; StringBuilder sb = new StringBuilder(); sb.append(this.dayOfMonth + "."); sb.append(ISMIDate.gregorianMonths.get(this.month) + "."); sb.append(this.year); if(this.ambiguity != null && this.ambiguity > 0){ sb.append(" [+-" + this.ambiguity + "]"); } return sb.toString(); } public String toShortString() { return String.format("%04d-%02d-%02d", year, month, dayOfMonth); } public String toIslamicString(){ try{ if(year == null || month == null || dayOfMonth == null) return null; DateTime gr = new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); DateTime islamic = new DateTime(gr.withChronology(IslamicChronology.getInstance())); StringBuilder sb = new StringBuilder(); sb.append(islamic.getDayOfMonth() + "."); sb.append(ISMIDate.islamicMonths.get(islamic.getMonthOfYear()) + "."); sb.append(islamic.getYear()); if(!isDayInWeek(this.dayOfWeek) && !hasAmbiguity()){ sb.append(" [+-" + AMBIGUITY + "]"); } return sb.toString(); }catch (Exception e) {} return "no valid"; } public boolean hasAmbiguity(){ if(this.getAmbiguity() == null) return false; if(this.getAmbiguity() > 0) return true; return false; } public String toJulianString(){ try{ if(year == null || month == null || dayOfMonth == null) return null; DateTime gr = new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); DateTime julian = new DateTime(gr.withChronology(JulianChronology.getInstance())); StringBuilder sb = new StringBuilder(); sb.append(julian.getDayOfMonth() + "."); sb.append(ISMIDate.julianMonths.get(julian.getMonthOfYear()) + "."); sb.append(julian.getYear()); if(this.ambiguity != null && this.ambiguity > 0){ sb.append(" [+-" + this.ambiguity + "]"); } return sb.toString(); }catch (Exception e) {} return "no valid"; } public DateTime getDateTime() { if (year == null || month == null || dayOfMonth == null) return null; return new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); } public DateTime getIslamicDateTime(){ if(year == null || month == null || dayOfMonth == null) return null; try{ DateTime gr = new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); return new DateTime(gr.withChronology(IslamicChronology.getInstance())); }catch (Exception e) {} return null; } public DateTime getJulianDateTime(){ if(year == null || month == null || dayOfMonth == null) return null; try{ DateTime gr = new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance()); return new DateTime(gr.withChronology(JulianChronology.getInstance())); }catch (Exception e) {} return null; } public Integer getDayOfMonth() { return dayOfMonth; } public void setDayOfMonth(Integer dayOfMonth) { this.dayOfMonth = dayOfMonth; } public Integer getDayOfWeek() { return dayOfWeek; } public void setDayOfWeek(Integer dayOfWeek) { this.dayOfWeek = dayOfWeek; } public Integer getDayOfYear() { return dayOfYear; } public void setDayOfYear(Integer dayOfYear) { this.dayOfYear = dayOfYear; } public Integer getMonth() { return month; } public void setMonth(Integer month) { this.month = month; } public Integer getYear() { return year; } public void setYear(Integer year) { this.year = year; } public Integer getCentury() { return century; } public void setCentury(Integer century) { this.century = century; } public Integer getAmbiguity() { return ambiguity; } public void setAmbiguity(Integer ambiguity) { this.ambiguity = ambiguity; } }