Reputation: 59
Suppose my date format is 21/05/2017 then the output will be SUN.
How can I get the day given a date?
Upvotes: 3
Views: 14198
Reputation: 2128
You can also use nscala-time
which is a Scala wrapper for Java's joda-time
which is a datetime library with a ton of functionality.
import com.github.nscala_time.time.Imports._
val someDate =(newDateTime).withYear(2017)
.withMonthOfYear(5)
.withDayOfMonth(21)
someDate.getDayOfWeek
res7: Int = 7
Now you need to know the mapping between integer days of the week and names. Here, 7 corresponds to Sunday.
Upvotes: 1
Reputation: 22439
You can use SimpleDateFormat as illustrated below:
import java.util.Calendar
import java.text.SimpleDateFormat
val now = Calendar.getInstance.getTime
val date = new SimpleDateFormat("yyyy-MM-dd")
date.format(now)
res1: String = 2017-05-20
val dowInt = new SimpleDateFormat("u")
dowInt.format(now)
res2: String = 6
val dowText = new SimpleDateFormat("E")
dowText.format(now)
res3: String = Sat
[UPDATE]
As per comments below, please note that SimpleDateFormat
isn't thread-safe.
Upvotes: 4
Reputation: 1766
import java.time.LocalDate
import java.time.format.DateTimeFormatter
val df = DateTimeFormatter.ofPattern("dd/MM/yyyy")
val dayOfWeek = LocalDate.parse("21/05/2017",df).getDayOfWeek
Upvotes: 5