从纪元中获取星期几

Obtain Day of the week from epoch

我有纪元时间,我正在尝试获取星期几。例如,假设我得到的时间是 16/04/2015 16:03:56。我想知道 16 号是星期几(星期一,星期二...)

scala> import java.text.SimpleDateFormat
import java.text.SimpleDateFormat

scala> import java.util.{TimeZone, Locale}
import java.util.{TimeZone, Locale}

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"))
scala> val dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.US)

以下代码将 return 时间与日期:

scala> dateFormat.format("1429200236824".toLong)
res2: String = 16/04/2015 16:03:56

从这里我如何获得第 16 天是哪一天,(上面的例子是在 scala 中,但它也是 java)

Java 8 解

您应该使用新的 Java 8 DateTime API。它基于 JodaTime,使用起来更方便。这是对 API (http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html).

的一个很好的概述

使用新的API,下面的代码会得到你的答案。

Welcome to Scala version 2.11.7 (OpenJDK 64-Bit Server VM, Java 1.8.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import java.time.ZoneId
import java.time.ZoneId

scala> import java.time.ZonedDateTime
import java.time.ZonedDateTime

scala> import java.time.Instant
import java.time.Instant

scala> ZonedDateTime.ofInstant(Instant.ofEpochMilli("1429200236824".toLong), ZoneId.of("Etc/UTC")).getDayOfWeek
res0: java.time.DayOfWeek = THURSDAY

scala> 

Java 7 标准库解决方案

如果您必须使用 Java 7(您不应该使用 Java 7),那么这将根据 Int (1=Sunday, 7=星期六)。

Welcome to Scala version 2.11.7 (OpenJDK 64-Bit Server VM, Java 1.8.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import java.util.TimeZone
import java.util.TimeZone

scala> import java.util.Calendar
import java.util.Calendar

scala> val c = Calendar.getInstance
c: java.util.Calendar = java.util.GregorianCalendar[time=1445886305100,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Denver",offset=-25200000,dstSavings=3600000,useDaylight=true,transitions=157,lastRule=java.util.SimpleTimeZone[id=America/Denver,offset=-25200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=9,WEEK_OF_YEAR=44,WEEK_OF_MONTH=5,DAY_OF_MONTH=26,DAY_OF_YEAR=299,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=5,SECOND=5,MILLISECOND=100,ZONE_OFFSET=-25200000,DST_OFFSET=3600000]

scala> c.setTimeZone(TimeZone.getTimeZone("Etc/UTC"))

scala> c.setTimeInMillis("1429200236824".toLong)

scala> c.get(Calendar.DAY_OF_WEEK)
res2: Int = 5

scala> 

乔达时间

此处要求的是 Joda-Time 版本。 请注意,Joda-Time 开发人员要求您改用 Java 8 标准库。一旦 Joda-Time 生命周期结束,您将面临使用无法修复错误的库的危险,即 您应该使用 Java 8

Welcome to Scala version 2.11.7 (OpenJDK 64-Bit Server VM, Java 1.8.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.joda.time.{DateTimeZone, DateTime}
import org.joda.time.{DateTimeZone, DateTime}

scala> new DateTime("1429200236824".toLong, DateTimeZone.forID("Etc/UTC")).dayOfWeek.getAsText
warning: Class org.joda.convert.FromString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
warning: Class org.joda.convert.FromString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
res0: String = Thursday

scala>