从纪元获取星期几 [英] Obtain Day of the week from epoch

查看:126
本文介绍了从纪元获取星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个划时代的时间,我正在尝试获取星期几。例如,假设我有时间为16/04/2015 16:03:56。我想找出星期几是16日(星期一,星期二...)

I have epoch time and I am trying to get the day of the week. For example lets say I get time as 16/04/2015 16:03:56. I want to find out what day is 16th (Monday, Tuesday... )

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)

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

Following code will return time with the date:

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

通过该方法,我如何获取第16天的日期(上面的示例在scala中,但同样也是java)

From this how can I obtain what day is 16th, (above example is in scala but its same is java too)

推荐答案

Java 8解决方案



您应使用新的Java 8 DateTime API。它基于JodaTime,并且更好用。这是API的完整概述( http:// www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html )。

使用新的API,以下代码

Using the new API, the following code will get your answer.

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),然后才能获得一周中的整数值(1 =星期日,7 =星期六)。

Java 7 Standard Library Solution

If you must use Java 7 (you shouldn't use Java 7) then this will get you the day of the week in terms of an Int (1=Sunday, 7=Saturday).

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 版本。 请注意,Joda-Time开发人员要求您改为使用Java 8标准库。一旦他们的生命终止,Joda-Time将有使用无法修复错误的库的危险,即您应该使用Java 8

Joda-Time

As requested here is a Joda-Time version. Please be aware that the Joda-Time developers are asking you to use the Java 8 Standard Library instead. Once they end of life Joda-Time you will be in danger of using a library that will not get bug fixes, i.e. You should use 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> 

这篇关于从纪元获取星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆