calendar.get(Calendar.MONTH) 月份是从0开始的 你设置是12 其实系统以为是1月,所以用0来表示1月,你如果想表示12月那就应该设置calendar.set(2011,11,31); 这样就会读出来是数字12
之前没有写过这样的函数,结果在HIVE里面查找分区,结果发现经常出错,其原因居然在于这个地方。后面直接+1就成功解决了。
scala> import java.util.{Calendar, Date} import java.util.{Calendar, Date} scala> val cal = Calendar.getInstance() cal: java.util.Calendar = java.util.GregorianCalendar[time=1459391506269,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=2,WEEK_OF_YEAR=14,WEEK_OF_MONTH=5,DAY_OF_MONTH=31,DAY_OF_YEAR=91,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=31,SECOND=46,MILLISECOND=269,ZONE_OFFSET=28800000,DST_OFFSET=0] scala> cal.setTimeInMillis(ts*1000L) scala> val year:Int = cal.get(Calendar.YEAR) year: Int = 2015 scala> val month:Int = cal.get(Calendar.MONTH) month: Int = 9 scala> val day1:Int = cal.get(Calendar.DAY_OF_MONTH) day1: Int = 2 scala> val ts2 = 1441123200L; ts2: Long = 1441123200 scala> cal.setTimeInMillis(ts2*1000L) scala> val month:Int = cal.get(Calendar.MONTH) month: Int = 8 scala> val ts3=1420041600L ts3: Long = 1420041600 scala> cal.setTimeInMillis(ts3*1000L) scala> val month:Int = cal.get(Calendar.MONTH) month: Int = 0