jdk/src/share/classes/sun/util/calendar/LocalGregorianCalendar.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8543 e5ec12a932da
child 11130 c7093e306a34
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8543
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.util.calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Masayoshi Okutsu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class LocalGregorianCalendar extends BaseCalendar {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Era[] eras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static class Date extends BaseCalendar.Date {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        protected Date() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        protected Date(TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            super(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        private int gregorianYear = FIELD_UNDEFINED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public Date setEra(Era era) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (getEra() != era) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                super.setEra(era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                gregorianYear = FIELD_UNDEFINED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public Date addYear(int localYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            super.addYear(localYear);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            gregorianYear += localYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public Date setYear(int localYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (getYear() != localYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                super.setYear(localYear);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                gregorianYear = FIELD_UNDEFINED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public int getNormalizedYear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return gregorianYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public void setNormalizedYear(int normalizedYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            this.gregorianYear = normalizedYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        void setLocalEra(Era era) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            super.setEra(era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        void setLocalYear(int year) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            super.setYear(year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            String time = super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            time = time.substring(time.indexOf('T'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            Era era = getEra();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (era != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                String abbr = era.getAbbreviation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (abbr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    sb.append(abbr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            sb.append(getYear()).append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            CalendarUtils.sprintf0d(sb, getMonth(), 2).append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            CalendarUtils.sprintf0d(sb, getDayOfMonth(), 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            sb.append(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static LocalGregorianCalendar getLocalGregorianCalendar(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Properties calendarProps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            String homeDir = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                new sun.security.action.GetPropertyAction("java.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            final String fname = homeDir + File.separator + "lib" + File.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                 + "calendars.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            calendarProps = (Properties) AccessController.doPrivileged(new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                public Object run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    Properties props = new Properties();
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   130
                    try (FileInputStream fis = new FileInputStream(fname)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   131
                        props.load(fis);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
   132
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    return props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new RuntimeException(e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // Parse calendar.*.eras
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String props = calendarProps.getProperty("calendar." + name + ".eras");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (props == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        List<Era> eras = new ArrayList<Era>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        StringTokenizer eraTokens = new StringTokenizer(props, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while (eraTokens.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            String items = eraTokens.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            StringTokenizer itemTokens = new StringTokenizer(items, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            String eraName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            boolean localTime = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            long since = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            String abbr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            while (itemTokens.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                String item = itemTokens.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                int index = item.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // it must be in the key=value form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                String key = item.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                String value = item.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if ("name".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    eraName = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                } else if ("since".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    if (value.endsWith("u")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        localTime = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        since = Long.parseLong(value.substring(0, value.length() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        since = Long.parseLong(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                } else if ("abbr".equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    abbr = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    throw new RuntimeException("Unknown key word: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            Era era = new Era(eraName, abbr, since, localTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            eras.add(era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Era[] eraArray = new Era[eras.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        eras.toArray(eraArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return new LocalGregorianCalendar(name, eraArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private LocalGregorianCalendar(String name, Era[] eras) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.eras = eras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        setEras(eras);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public Date getCalendarDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return getCalendarDate(System.currentTimeMillis(), newCalendarDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Date getCalendarDate(long millis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return getCalendarDate(millis, newCalendarDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public Date getCalendarDate(long millis, TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return getCalendarDate(millis, newCalendarDate(zone));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public Date getCalendarDate(long millis, CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Date ldate = (Date) super.getCalendarDate(millis, date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return adjustYear(ldate, millis, ldate.getZoneOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private Date adjustYear(Date ldate, long millis, int zoneOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        for (i = eras.length - 1; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            Era era = eras[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            long since = era.getSince(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (era.isLocalTime()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                since -= zoneOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (millis >= since) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                ldate.setLocalEra(era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                int y = ldate.getNormalizedYear() - era.getSinceDate().getYear() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                ldate.setLocalYear(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (i < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            ldate.setLocalEra(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            ldate.setLocalYear(ldate.getNormalizedYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        ldate.setNormalized(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return ldate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public Date newCalendarDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public Date newCalendarDate(TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return new Date(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public boolean validate(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Date ldate = (Date) date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Era era = ldate.getEra();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (era != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (!validateEra(era)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            ldate.setNormalizedYear(era.getSinceDate().getYear() + ldate.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            ldate.setNormalizedYear(ldate.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return super.validate(ldate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private boolean validateEra(Era era) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // Validate the era
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (int i = 0; i < eras.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (era == eras[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public boolean normalize(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (date.isNormalized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        normalizeYear(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Date ldate = (Date) date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Normalize it as a Gregorian date and get its millisecond value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        super.normalize(ldate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        boolean hasMillis = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        long millis = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int year = ldate.getNormalizedYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        Era era = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        for (i = eras.length - 1; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            era = eras[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (era.isLocalTime()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                CalendarDate sinceDate = era.getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                int sinceYear = sinceDate.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (year > sinceYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (year == sinceYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    int month = ldate.getMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    int sinceMonth = sinceDate.getMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    if (month > sinceMonth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    if (month == sinceMonth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        int day = ldate.getDayOfMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        int sinceDay = sinceDate.getDayOfMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        if (day > sinceDay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        if (day == sinceDay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            long timeOfDay = ldate.getTimeOfDay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            long sinceTimeOfDay = sinceDate.getTimeOfDay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            if (timeOfDay >= sinceTimeOfDay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            --i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (!hasMillis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    millis  = super.getTime(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    hasMillis = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                long since = era.getSince(date.getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (millis >= since) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            ldate.setLocalEra(era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            int y = ldate.getNormalizedYear() - era.getSinceDate().getYear() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            ldate.setLocalYear(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // Set Gregorian year with no era
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            ldate.setEra(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            ldate.setLocalYear(year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            ldate.setNormalizedYear(year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        ldate.setNormalized(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    void normalizeMonth(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        normalizeYear(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        super.normalizeMonth(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    void normalizeYear(CalendarDate date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Date ldate = (Date) date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Set the supposed-to-be-correct Gregorian year first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // e.g., Showa 90 becomes 2015 (1926 + 90 - 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        Era era = ldate.getEra();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (era == null || !validateEra(era)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            ldate.setNormalizedYear(ldate.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            ldate.setNormalizedYear(era.getSinceDate().getYear() + ldate.getYear() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Returns whether the specified Gregorian year is a leap year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @see #isLeapYear(Era, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public boolean isLeapYear(int gregorianYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return CalendarUtils.isGregorianLeapYear(gregorianYear);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public boolean isLeapYear(Era era, int year) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (era == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return isLeapYear(year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int gyear = era.getSinceDate().getYear() + year - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return isLeapYear(gyear);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public void getCalendarDateFromFixedDate(CalendarDate date, long fixedDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        Date ldate = (Date) date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        super.getCalendarDateFromFixedDate(ldate, fixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        adjustYear(ldate, (fixedDate - EPOCH_OFFSET) * DAY_IN_MILLIS, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
}