| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.util; |
| 2 | |
| 3 | import java.util.*; |
| 4 | import java.util.regex.*; |
| 5 | |
| 6 | /** |
| 7 | * @author Nils Diewald |
| 8 | * |
| 9 | * KorapDate implements a helper object to stringify and parse date strings implemented |
| 10 | * for integer range queries. |
| 11 | */ |
| 12 | public class KorapDate { |
| 13 | /* |
| 14 | protected char[] year = new char[4]; |
| 15 | protected char[] month = new char[2]; |
| 16 | protected char[] day = new char[2]; |
| 17 | */ |
| 18 | |
| 19 | private int year = 0, month = 0, day = 0; |
| 20 | |
| 21 | private static final Pattern datePattern = Pattern.compile( |
| 22 | "(\\d\\d\\d\\d)" + |
| 23 | "(?:[-/]?(\\d\\d)" + |
| 24 | "(?:[-/]?(\\d\\d))?)?" |
| 25 | ); |
| 26 | |
| 27 | public static int END = 99_999_999; |
| 28 | public static int BEGINNING = 0; |
| 29 | |
| 30 | public KorapDate (String dateStr) { |
| 31 | if (dateStr == null || dateStr.isEmpty()) |
| 32 | return; |
| 33 | |
| 34 | Matcher m = datePattern.matcher(dateStr); |
| 35 | if (m.matches()) { |
| 36 | this.year = Integer.parseInt(m.group(1)); |
| 37 | if (m.group(2) != null) |
| 38 | this.month = Integer.parseInt(m.group(2)); |
| 39 | if (m.group(3) != null) |
| 40 | this.day = Integer.parseInt(m.group(3)); |
| 41 | } |
| 42 | else { |
| 43 | return; |
| 44 | }; |
| 45 | }; |
| 46 | |
| 47 | private static int ceil (short padding, int nr) { |
| 48 | if (nr == 0) { |
| 49 | if (padding == (short) 4) { |
| 50 | return 9999; |
| 51 | } |
| 52 | else if (padding == (short) 2) { |
| 53 | return 99; |
| 54 | }; |
| 55 | }; |
| 56 | return nr; |
| 57 | }; |
| 58 | |
| 59 | // make yyyy???? become yyyy9999 and yyyymm?? yyyymm99 |
| 60 | public int ceil () { |
| 61 | return |
| 62 | (ceil((short) 4, this.year) * 10_000) + |
| 63 | (ceil((short) 2, this.month) * 100) + |
| 64 | (ceil((short) 2, this.day)); |
| 65 | }; |
| 66 | |
| 67 | // make yyyy???? become yyyy0000 and yyyymm?? yyyymm00 |
| 68 | public int floor () { |
| 69 | int floor = 0; |
| 70 | if (this.year == 0) { |
| 71 | return 0; |
| 72 | } |
| 73 | else { |
| 74 | floor = this.year * 10_000; |
| 75 | }; |
| 76 | if (this.month == 0) { |
| 77 | return floor; |
| 78 | } |
| 79 | else { |
| 80 | floor += this.month * 100; |
| 81 | }; |
| 82 | if (this.day == 0) { |
| 83 | return floor; |
| 84 | }; |
| 85 | return (floor + this.day); |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | public int year () { |
| 90 | return this.year; |
| 91 | }; |
| 92 | |
| 93 | public int month () { |
| 94 | return this.month; |
| 95 | }; |
| 96 | |
| 97 | public int day () { |
| 98 | return this.day; |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | public String toString() { |
| 103 | StringBuilder sb = this.toStringBuilder(); |
| 104 | if (sb.length() < 4) |
| 105 | return null; |
| 106 | |
| 107 | if (sb.length() < 8) { |
| 108 | sb.append("00"); |
| 109 | if (sb.length() < 6) { |
| 110 | sb.append("00"); |
| 111 | }; |
| 112 | }; |
| 113 | |
| 114 | return sb.toString(); |
| 115 | }; |
| 116 | |
| 117 | public String toDisplay() { |
| 118 | StringBuilder sb = this.toStringBuilder(); |
| 119 | if (sb.length() == 8) |
| 120 | sb.insert(6, '-'); |
| 121 | |
| 122 | if (sb.length() > 4) |
| 123 | sb.insert(4, '-'); |
| 124 | |
| 125 | return sb.toString(); |
| 126 | }; |
| 127 | |
| 128 | public String toCeilString() { |
| 129 | StringBuilder sb = new StringBuilder(); |
| 130 | return sb.append(this.ceil()).toString(); |
| 131 | }; |
| 132 | |
| 133 | public String toFloorString() { |
| 134 | StringBuilder sb = new StringBuilder(); |
| 135 | return sb.append(this.floor()).toString(); |
| 136 | }; |
| 137 | |
| 138 | // Format date as yyyymmdd |
| 139 | private StringBuilder toStringBuilder () { |
| 140 | StringBuilder sb = new StringBuilder(); |
| 141 | if (this.year != 0) { |
| 142 | |
| 143 | // Append year |
| 144 | if (this.year < 100) |
| 145 | sb.append("20"); |
| 146 | |
| 147 | sb.append(this.year); |
| 148 | |
| 149 | if (this.month != 0) { |
| 150 | |
| 151 | // Append month |
| 152 | if (this.month < 10) |
| 153 | sb.append('0'); |
| 154 | sb.append(this.month); |
| 155 | |
| 156 | if (this.day != 0) { |
| 157 | // Append month |
| 158 | if (this.day < 10) |
| 159 | sb.append('0'); |
| 160 | sb.append(this.day); |
| 161 | }; |
| 162 | }; |
| 163 | }; |
| 164 | return sb; |
| 165 | }; |
| 166 | }; |