从纪元获得星期几

时间:2015-10-26 18:38:21

标签: java scala 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)

以下代码将返回日期时间:

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

从这个我怎样才能获得第16天(上面的例子是scala,但同样是java)

1 个答案:

答案 0 :(得分:2)

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 =星期日,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>