scala 如何在scala中获取当前日期,月份,年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39771252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current date, month, year in scala
提问by toofrellik
i need current year, month & date to 3 different variables. below code gives date time
我需要当前的年份、月份和日期到 3 个不同的变量。下面的代码给出了日期时间
val now = Calendar.getInstance().getTime()
Thu Sep 29 18:27:38 IST 2016
2016 年 9 月 29 日星期四 18:27:38 IST
but i need in YYYY MM and DD format
但我需要 YYYY MM 和 DD 格式
回答by toofrellik
val cal = Calendar.getInstance()
val date =cal.get(Calendar.DATE )
val Year =cal.get(Calendar.YEAR )
val Month1 =cal.get(Calendar.MONTH )
val Month = Month1+1
Month is added with one because month starts with zero index.
月份加 1,因为月份从零索引开始。
回答by Sarvesh Kumar Singh
When you are talking about year, month, day-of-month, hour-of-dayetc... your timezone becomes very important. So whenever you are talking about all these, you have to mention the timezone.
当你在谈论year,month,day-of-month,hour-of-day等...你的时区变得非常重要。所以每当你谈论所有这些时,你必须提到时区。
If you are using java 8, you can use the java.time api
如果您使用的是 java 8,则可以使用 java.time api
import java.time.{ZonedDateTime, ZonedOffset}
// val yourTimeZoneOffset = ZoneOffset.ofHoursMinutesSeconds(hour, minute, second)
// for example IST or Indian Standard Time has an offset of "+05:30:00" hrs
val istOffset = ZoneOffset.ofHoursMinutesSeconds(5, 30, 0)
// istOffset: java.time.ZoneOffset = +05:30
// time representation in IST
val zonedDateTimeIst = ZonedDateTime.now(istOffset)
// zonedDateTimeIst: java.time.ZonedDateTime = 2016-09-29T20:14:48.048+05:30
val year = zonedDateTimeIst.getYear
// year: Int = 2016
val month = zonedDateTimeIst.getMonth
// month: java.time.Month = SEPTEMBER
val dayOfMonth = zonedDateTimeIst.getDayOfMonth
// dayOfMonth: Int = 29
val df1 = DateTimeFormatter.ofPattern("yyyy - MM - dd")
val df2 = DateTimeFormatter.ofPattern("yyyy/MM/dd")
// df1: java.time.format.DateTimeFormatter = Value(YearOfEra,4,19,EXCEEDS_PAD)' ''-'' 'Value(MonthOfYear,2)' ''-'' 'Value(DayOfMonth,2)
// df2: java.time.format.DateTimeFormatter = Value(YearOfEra,4,19,EXCEEDS_PAD)'/'Value(MonthOfYear,2)'/'Value(DayOfMonth,2)
val dateString1 = zonedDateTimeIst.format(df1)
// dateString1: String = 2016 - 09 - 29
val dateString2 = zonedDateTimeIst.format(df2)
// dateString2: String = 2016/09/29
回答by alwe
You could use joda-time:
你可以使用joda-time:
import org.joda.time.DateTime
val date: String = DateTimeFormat.forPattern("yyyy-MM-dd").print(DateTime.now())
val month: Int = DateTime.now().getMonthOfYear
val year: Int = DateTime.now().getYear
In case you use java8 you could also use the native DateTime API, which is designed after the joda-time api.
如果您使用 java8,您还可以使用本机 DateTime API,它是在 joda-time api 之后设计的。
回答by Will
You need to use different SimpleDateFormat objects for that purpose as described here
您需要使用不同的SimpleDateFormat对象为目的的描述在这里
In your case, you'll need :
在您的情况下,您需要:
import java.text.SimpleDateFormat
val minuteFormat = new SimpleDateFormat("MM")
val yearFormat = new SimpleDateFormat("YYYY")
val dayFormat = new SimpleDateFormat("dd")
And then, to get for example, the year for a given date, simply use :
然后,例如,要获取给定日期的年份,只需使用:
val currentYear = hourFormat.format(yourDate)

