我試圖將一年中的當前日期作為整數來訪問我的程式。我查看了 Kotlin Docs 并找到了一個名為 的函式getDay(),但是當我將它輸入到我的程式中時,它給了我一個錯誤并說該函式未定義。我正在使用帶有 Kotlin 的 Android Studio,并且最小 API 是 21。

uj5u.com熱心網友回復:
時間
該java.util日期時間API及API格式,SimpleDateFormat都已經過時,而且容易出錯。建議完全停止使用它們并切換到現代 Date-Time API *。
使用java.time現代日期時間 API 的解決方案:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Note: If you want to work with a specific timezone, use LocalDate.now(ZoneId)
// e.g. LocalDate.now(ZoneId.of("Australia/Brisbane")) or
// LocalDate.now(ZoneOffset.UTC) etc.
LocalDate today = LocalDate.now();
int doy = today.getDayOfYear();
System.out.println(doy);
// Using DateTimeFormatter (not recommended for production code)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("D", Locale.ENGLISH);
String strDoy = today.format(dtf);
System.out.println(strDoy);
}
}
輸出在我的時區,歐洲/倫敦:
289
289
ONLINE DEMO
從Trail: Date Time 中了解有關現代日期時間 API 的更多資訊。
無論出于何種原因,如果您想使用舊 API:
一個java.util.Date物件只是代表時間線上的一個瞬間——一個包裝自 UNIX 時代(1970 年 1 月 1 日,格林威治標準時間 00:00:00)以來的毫秒數。由于它不保存任何時區資訊,因此它的toString函式應用 JVM 的時區以從該毫秒值派生String的格式回傳。要以不同的格式和時區表示物件,您需要使用所需的格式和適用的時區,例如EEE MMM dd HH:mm:ss zzz yyyyStringjava.util.DateSimpleDateFormat
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String strDateNewYork = sdf.format(date);
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String strDateUtc = sdf.format(date);
讓我們看看為什么知道上述事實很重要:
The function, Calendar#getInstance returns a calendar based on the current time in the default time zone with the default FORMAT locale. If you want to find some information from it for some other timezone, you have two options:
- Change the default timezone e.g.
TimeZone.setDefault(TimeZone.getTimeZone("Australia/Brisbane")). However, this may result in other parts of the application behave incorrectly. Therefore, this option is strongly discouraged. - Format the
java.util.Date(which you can get viaCalendar#getTime) usingSimpleDateFormatset with the required timezone shown above.
Demo:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("D");
// For the JVM's timezone
sdf.setTimeZone(TimeZone.getDefault());
System.out.println(sdf.format(now));
// For the timezone, UTC
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(now));
// For the timezone, Australia/Brisbane
sdf.setTimeZone(TimeZone.getTimeZone("Australia/Brisbane"));
System.out.println(sdf.format(now));
System.out.println();
}
}
Output in my timezone, Europe/London at the time of posting this answer:
289
289
290
ONLINE DEMO
* 如果您正在為 Android 專案作業并且您的 Android API 級別仍然不符合 Java-8,請檢查通過 desugaring 可用的 Java 8 API。請注意,Android 8.0 Oreo 已提供對java.time.
uj5u.com熱心網友回復:
Date().getDay()或者更確切地說,Date().day在 Kotlin 中回傳星期幾,所以不是您想要的。
Calendar.getInstance().get(Calendar.DAY_OF_YEAR)是在 Android 中使用的正確函式。它使用默認時區和語言環境回傳日歷。回傳的日歷基于默認時區中的當前時間和默認 FORMAT 語言環境 ( https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance-- ) .
Calendar,Date等已換成新的java.time類與Java 8所以你應該使用LocalDate.now().dayOfYear,但作為@nuhkoca表示為了做到這一點,你需要啟用Java API 8 支持脫糖被添加基本上這構建檔案(科特林DSL Groovy的不):
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")
uj5u.com熱心網友回復:
如果您執行脫糖以便能夠使用Java 8 Time API回 API 21,則可以使用此方法獲取一年中的第幾天
import java.time.LocalDate
val dayOfYear = LocalDate.now().dayOfYear
System.out.println(dayOfYear);
答案:289
uj5u.com熱心網友回復:
試試這個代碼:
import java.time.LocalDate
import java.util.*
fun main() {
val cal = Calendar.getInstance()
val day = cal[Calendar.DATE]
val doy = cal[Calendar.DAY_OF_YEAR]
println("Current Date: " cal.time)
println("Day : $day")
println("Day of Year : $doy")
}
uj5u.com熱心網友回復:
這個 getDay() 方法是 Date 類的一部分。檢查這個:https : //kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-date/get-day.html
您必須執行以下操作:
val currentDay = Date().getDay()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/331449.html
上一篇:使用Moment.JS計算不同格式的兩個日期之間的差異
下一篇:SAS-如何讀取日期資料?
