1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.core.i18n 18 19 import androidx.annotation.IntDef 20 import androidx.annotation.RestrictTo 21 import java.text.DateFormat 22 23 /** Date/time formatting styles, compatible to the ones in [java.text.DateFormat] */ 24 @RestrictTo(RestrictTo.Scope.LIBRARY) 25 @Target(AnnotationTarget.TYPE) 26 @Retention(AnnotationRetention.SOURCE) 27 @IntDef( 28 /** SHORT is completely numeric, such as 12.13.52 or 3:30pm. */ 29 DateFormat.SHORT, 30 /** MEDIUM is longer, such as Jan 12, 1952. */ 31 DateFormat.MEDIUM, 32 /** LONG is longer, such as January 12, 1952 or 3:30:32pm. */ 33 DateFormat.LONG, 34 /** FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST. */ 35 DateFormat.FULL 36 ) 37 public annotation class DateTimeStyle 38 39 /** 40 * This class provides functionality similar to the one in [java.text.DateFormat]. 41 * 42 * Although not very flexible, it makes migration easier. 43 */ 44 public class DateTimeFormatterJdkStyleOptions 45 private constructor( 46 public val dateStyle: @DateTimeStyle Int, 47 public val timeStyle: @DateTimeStyle Int 48 ) { 49 public companion object { 50 /** 51 * Gets the date formatter with the given formatting style for the default locale. 52 * 53 * @param style the given date formatting style. For example, SHORT for "M/d/yy" in the US 54 * locale. 55 * @return the formatting options to use with [androidx.core.i18n.DateTimeFormatter]. 56 */ 57 @JvmStatic createDateInstancenull58 public fun createDateInstance(style: @DateTimeStyle Int): DateTimeFormatterJdkStyleOptions { 59 return DateTimeFormatterJdkStyleOptions(style, -1) 60 } 61 62 /** 63 * Gets the time formatter with the given formatting style for the default locale. 64 * 65 * @param style the given time formatting style. For example, SHORT for "h:mm a" in the US 66 * locale. 67 * @return the formatting options to use with [androidx.core.i18n.DateTimeFormatter]. 68 */ 69 @JvmStatic createTimeInstancenull70 public fun createTimeInstance(style: @DateTimeStyle Int): DateTimeFormatterJdkStyleOptions { 71 return DateTimeFormatterJdkStyleOptions(-1, style) 72 } 73 74 /** 75 * Gets the date / time formatter with the given formatting styles for the default locale. 76 * 77 * @param dateStyle the given date formatting style. For example, SHORT for "M/d/yy" in the 78 * US locale. 79 * @param timeStyle the given time formatting style. For example, SHORT for "h:mm a" in the 80 * US locale. 81 * @return the formatting options to use with [androidx.core.i18n.DateTimeFormatter]. 82 */ 83 @JvmStatic createDateTimeInstancenull84 public fun createDateTimeInstance( 85 dateStyle: @DateTimeStyle Int, 86 timeStyle: @DateTimeStyle Int 87 ): DateTimeFormatterJdkStyleOptions { 88 return DateTimeFormatterJdkStyleOptions(dateStyle, timeStyle) 89 } 90 } 91 } 92