| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.date.internal; |
| 20 | import static org.jclouds.date.internal.DateUtils.trimNanosToMillis; |
| 21 | import static org.jclouds.date.internal.DateUtils.trimTZ; |
| 22 | |
| 23 | import java.text.ParseException; |
| 24 | import java.text.SimpleDateFormat; |
| 25 | import java.util.Date; |
| 26 | import java.util.Locale; |
| 27 | import java.util.SimpleTimeZone; |
| 28 | |
| 29 | import org.jclouds.date.DateService; |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | * uses {@link SimpleDateFormat} internally. |
| 34 | * |
| 35 | * @author Adrian Cole |
| 36 | * @author James Murty |
| 37 | */ |
| 38 | public class SimpleDateFormatDateService implements DateService { |
| 39 | |
| 40 | /* |
| 41 | * Use default Java Date/SimpleDateFormat classes for date manipulation, but be *very* careful to |
| 42 | * guard against the lack of thread safety. |
| 43 | */ |
| 44 | // @GuardedBy("this") |
| 45 | private static final SimpleDateFormat iso8601SecondsSimpleDateFormat = new SimpleDateFormat( |
| 46 | "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); |
| 47 | |
| 48 | // @GuardedBy("this") |
| 49 | private static final SimpleDateFormat iso8601SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", |
| 50 | Locale.US); |
| 51 | |
| 52 | // @GuardedBy("this") |
| 53 | private static final SimpleDateFormat rfc822SimpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", |
| 54 | Locale.US); |
| 55 | |
| 56 | // @GuardedBy("this") |
| 57 | private static final SimpleDateFormat cSimpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss '+0000' yyyy", |
| 58 | Locale.US); |
| 59 | |
| 60 | static { |
| 61 | iso8601SimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); |
| 62 | iso8601SecondsSimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); |
| 63 | rfc822SimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); |
| 64 | cSimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); |
| 65 | } |
| 66 | |
| 67 | public final Date fromSeconds(long seconds) { |
| 68 | return new Date(seconds * 1000); |
| 69 | } |
| 70 | |
| 71 | public final String cDateFormat(Date date) { |
| 72 | synchronized (cSimpleDateFormat) { |
| 73 | return cSimpleDateFormat.format(date); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public final String cDateFormat() { |
| 78 | return cDateFormat(new Date()); |
| 79 | } |
| 80 | |
| 81 | public final Date cDateParse(String toParse) { |
| 82 | synchronized (cSimpleDateFormat) { |
| 83 | try { |
| 84 | return cSimpleDateFormat.parse(toParse); |
| 85 | } catch (ParseException e) { |
| 86 | throw new RuntimeException(e); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public final String rfc822DateFormat(Date date) { |
| 92 | synchronized (rfc822SimpleDateFormat) { |
| 93 | return rfc822SimpleDateFormat.format(date); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public final String rfc822DateFormat() { |
| 98 | return rfc822DateFormat(new Date()); |
| 99 | } |
| 100 | |
| 101 | public final Date rfc822DateParse(String toParse) { |
| 102 | synchronized (rfc822SimpleDateFormat) { |
| 103 | try { |
| 104 | return rfc822SimpleDateFormat.parse(toParse); |
| 105 | } catch (ParseException e) { |
| 106 | throw new RuntimeException(e); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public final String iso8601SecondsDateFormat() { |
| 112 | return iso8601SecondsDateFormat(new Date()); |
| 113 | } |
| 114 | |
| 115 | public final String iso8601DateFormat(Date date) { |
| 116 | synchronized (iso8601SimpleDateFormat) { |
| 117 | return iso8601SimpleDateFormat.format(date); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public final String iso8601DateFormat() { |
| 122 | return iso8601DateFormat(new Date()); |
| 123 | } |
| 124 | |
| 125 | public final Date iso8601DateParse(String toParse) { |
| 126 | toParse = trimTZ(toParse); |
| 127 | toParse = trimNanosToMillis(toParse); |
| 128 | synchronized (iso8601SimpleDateFormat) { |
| 129 | try { |
| 130 | return iso8601SimpleDateFormat.parse(toParse); |
| 131 | } catch (ParseException e) { |
| 132 | throw new RuntimeException(e); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | public final Date iso8601SecondsDateParse(String toParse) { |
| 138 | toParse = trimTZ(toParse); |
| 139 | synchronized (iso8601SecondsSimpleDateFormat) { |
| 140 | try { |
| 141 | return iso8601SecondsSimpleDateFormat.parse(toParse); |
| 142 | } catch (ParseException e) { |
| 143 | throw new RuntimeException(e); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public String iso8601SecondsDateFormat(Date date) { |
| 150 | synchronized (iso8601SecondsSimpleDateFormat) { |
| 151 | return iso8601SecondsSimpleDateFormat.format(date); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } |