| 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.joda; |
| 20 | |
| 21 | import static org.jclouds.date.internal.DateUtils.trimNanosToMillis; |
| 22 | import static org.jclouds.date.internal.DateUtils.trimTZ; |
| 23 | |
| 24 | import java.util.Date; |
| 25 | import java.util.Locale; |
| 26 | |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.date.DateService; |
| 30 | import org.joda.time.DateTime; |
| 31 | import org.joda.time.DateTimeZone; |
| 32 | import org.joda.time.format.DateTimeFormat; |
| 33 | import org.joda.time.format.DateTimeFormatter; |
| 34 | |
| 35 | /** |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | * @author James Murty |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class JodaDateService implements DateService { |
| 42 | |
| 43 | private static final DateTimeFormatter rfc822DateFormatter = DateTimeFormat.forPattern( |
| 44 | "EEE, dd MMM yyyy HH:mm:ss 'GMT'").withLocale(Locale.US).withZone(DateTimeZone.forID("GMT")); |
| 45 | |
| 46 | private static final DateTimeFormatter cDateFormatter = DateTimeFormat |
| 47 | .forPattern("EEE MMM dd HH:mm:ss '+0000' yyyy").withLocale(Locale.US).withZone(DateTimeZone.forID("GMT")); |
| 48 | |
| 49 | private static final DateTimeFormatter iso8601SecondsDateFormatter = DateTimeFormat.forPattern( |
| 50 | "yyyy-MM-dd'T'HH:mm:ss'Z'").withLocale(Locale.US).withZone(DateTimeZone.forID("GMT")); |
| 51 | |
| 52 | private static final DateTimeFormatter iso8601DateFormatter = DateTimeFormat.forPattern( |
| 53 | "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withLocale(Locale.US).withZone(DateTimeZone.forID("GMT")); |
| 54 | |
| 55 | public final Date fromSeconds(long seconds) { |
| 56 | return new Date(seconds * 1000); |
| 57 | } |
| 58 | |
| 59 | public final String cDateFormat(Date dateTime) { |
| 60 | return cDateFormatter.print(new DateTime(dateTime)); |
| 61 | } |
| 62 | |
| 63 | public final String cDateFormat() { |
| 64 | return cDateFormat(new Date()); |
| 65 | } |
| 66 | |
| 67 | public final Date cDateParse(String toParse) { |
| 68 | return cDateFormatter.parseDateTime(toParse).toDate(); |
| 69 | } |
| 70 | |
| 71 | public final String rfc822DateFormat(Date dateTime) { |
| 72 | return rfc822DateFormatter.print(new DateTime(dateTime)); |
| 73 | } |
| 74 | |
| 75 | public final String rfc822DateFormat() { |
| 76 | return rfc822DateFormat(new Date()); |
| 77 | } |
| 78 | |
| 79 | public final Date rfc822DateParse(String toParse) { |
| 80 | return rfc822DateFormatter.parseDateTime(toParse).toDate(); |
| 81 | } |
| 82 | |
| 83 | public final String iso8601SecondsDateFormat(Date dateTime) { |
| 84 | return iso8601SecondsDateFormatter.print(new DateTime(dateTime)); |
| 85 | } |
| 86 | |
| 87 | public final String iso8601SecondsDateFormat() { |
| 88 | return iso8601SecondsDateFormat(new Date()); |
| 89 | } |
| 90 | |
| 91 | public final String iso8601DateFormat(Date date) { |
| 92 | return iso8601DateFormatter.print(new DateTime(date)); |
| 93 | } |
| 94 | |
| 95 | public final String iso8601DateFormat() { |
| 96 | return iso8601DateFormat(new Date()); |
| 97 | } |
| 98 | |
| 99 | public final Date iso8601DateParse(String toParse) { |
| 100 | toParse = trimTZ(toParse); |
| 101 | toParse = trimNanosToMillis(toParse); |
| 102 | return iso8601DateFormatter.parseDateTime(toParse).toDate(); |
| 103 | } |
| 104 | |
| 105 | public final Date iso8601SecondsDateParse(String toParse) { |
| 106 | toParse = trimTZ(toParse); |
| 107 | return iso8601SecondsDateFormatter.parseDateTime(toParse).toDate(); |
| 108 | } |
| 109 | } |