我在scala中有以下代码:
import java.util.Calendar
var calendar = Calendar.getInstance();
var lastTimeProcessed = calendar.set(Calendar.HOUR_OF_DAY, 0)
在spark-shell中,创建lastTimeProcessed之后,看起来这个变量是Unit,但我想要的是初始化为00:00。
这是控制台结果:
lastTimeProcessed: Unit = ()
我做错了什么?
答案 0 :(得分:1)
set
会改变实例的状态并返回Unit
作为输出类型。
calendar
的状态已更改。现在打印calendar
以查看新的变异状态。
calendar.getTime
设置HOUR_OF_DAY
scala> calendar.get(Calendar.HOUR_OF_DAY)
res12: Int = 0
答案 1 :(得分:1)
日历集返回无效但它确实会根据此次运行更改HOUR_OF_DAY或MINUTE的值:
scala> import java.util.Calendar
import java.util.Calendar
scala> var calendar = Calendar.getInstance();
calendar: java.util.Calendar = java.util.GregorianCalendar[time=1474194792722,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Manila",offset=28800000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=8,WEEK_OF_YEAR=39,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=262,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=33,SECOND=12,MILLISECOND=722,ZONE_OFFSET=28800000,DST_OFFSET=0]
scala> var lastTimeProcessed = calendar.set(Calendar.HOUR_OF_DAY, 0)
lastTimeProcessed: Unit = ()
scala> calendar
res0: java.util.Calendar = java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Manila",offset=28800000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=8,WEEK_OF_YEAR=39,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=262,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=6,HOUR_OF_DAY=0,MINUTE=33,SECOND=12,MILLISECOND=722,ZONE_OFFSET=28800000,DST_OFFSET=0]
scala> calendar.set(Calendar.MINUTE, 0)
scala> calendar
res2: java.util.Calendar = java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Manila",offset=28800000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=8,WEEK_OF_YEAR=39,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=262,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=6,HOUR_OF_DAY=0,MINUTE=0,SECOND=12,MILLISECOND=722,ZONE_OFFSET=28800000,DST_OFFSET=0]
未经请求的建议,请使用joda时间库。