简体   繁体   中英

Obtain Day of the week from epoch

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

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

Java 8 Solution

You should use the new Java 8 DateTime API. It is based on JodaTime and is much nicer to work with. Here is a good overview of the API ( http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html ).

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 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

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, ie 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM