我有一个外部API,我的日期为long
s,表示自Epoch以来的毫秒数。
使用旧式Java API,我只需使用
从中构建Date
Date myDate = new Date(startDateLong)
Java 8的LocalDate
/ LocalDateTime
类中的等价物是什么?
我有兴趣将长整数表示的时间点转换为当前本地时区的LocalDate
。
答案 0 :(得分:297)
如果您有自Epoch以来的毫秒数并希望使用当前本地时区将它们转换为本地日期,则可以使用
1, 6, 3, 9, 2, 7
但请记住,即使系统的默认时区可能会发生变化,因此相同的struct Defaults {
static let lastQuestionIndex = "lastQuestionIndex"
static let questionOrder = "questionOrder"
}
let questions: [Question] // array of model objects, always in fixed order
func nextQuestion() -> Question {
let defaults = NSUserDefaults.standardUserDefaults()
if let lastIndex = defaults.integerForKey(Defaults.lastQuestionIndex) {
// we've run before, load the ordering
guard let shuffledOrder = defaults.arrayForKey(Defaults.questionOrder) as? [Int]
else { fatalError("save questionOrder with lastQuestionIndex") }
// advance the saved index so the next call to this function
// will get the next question
if lastIndex + 1 < count {
defaults.setInteger(lastIndex + 1, forKey: Defaults.lastQuestionIndex)
} else {
// ran out of shuffled questions, forget the order so we
// can reshuffle on the next call
defaults.removeObjectForKey(Defaults.questionOrder)
defaults.removeObjectForKey(Defaults.lastQuestionIndex)
}
// map lastQuestionIndex from sequential to shuffled
// and return the corresponding answer
let shuffledIndex = shuffledOrder[lastIndex]
return questions[shuffledIndex]
} else {
// first run, shuffle the question ordering (not the actual questions)
let source = GKRandomSource()
let sequentialOrder = Array(0..<questions.count)
let shuffledOrder = source.arrayByShufflingObjectsInArray(sequentialOrder)
// save the ordering, and the fact that we're asking the first question
defaults.setObject(shuffledOrder, forKey: Defaults.questionOrder)
defaults.setInteger(0, forKey: Defaults.lastQuestionIndex)
// return the first question in the shuffled ordering
let shuffledIndex = shuffledOrder[0]
return questions[shuffledIndex]
}
}
值可能会在后续运行中产生不同的结果,即使在同一台计算机上也是如此。
此外,请注意NSUserDefaults
与LocalDate date =
Instant.ofEpochMilli(longValue).atZone(ZoneId.systemDefault()).toLocalDate();
不同,它实际上代表的是日期,而不是日期和时间。
否则,您可以使用long
:
LocalDate
答案 1 :(得分:27)
您可以从Instant.ofEpochMilli(long)开始:
LocalDate date =
Instant.ofEpochMilli(startDateLong)
.atZone(ZoneId.systemDefault())
.toLocalDate();
答案 2 :(得分:11)
我认为我有一个更好的答案。
new Timestamp(longEpochTime).toLocalDateTime();
答案 3 :(得分:2)
除了时区和其他内容,new Date(startDateLong)
的一个非常简单的替代方法可能是LocalDate.ofEpochDay(startDateLong / 86400000L)
答案 4 :(得分:2)
将now.getTime()替换为您的long值。
//GET UTC time for current date
Date now= new Date();
//LocalDateTime utcDateTimeForCurrentDateTime = Instant.ofEpochMilli(now.getTime()).atZone(ZoneId.of("UTC")).toLocalDateTime();
LocalDate localDate = Instant.ofEpochMilli(now.getTime()).atZone(ZoneId.of("UTC")).toLocalDate();
DateTimeFormatter dTF2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println(" formats as " + dTF2.format(utcDateTimeForCurrentDateTime));
答案 5 :(得分:-1)
在特定情况下,您的纪元时间戳记时间戳来自SQL或与SQL相关,您可以这样获得它:
long startDateLong = <...>
LocalDate theDate = new java.sql.Date(startDateLong).toLocalDate();