最近开始写scala程序,发现scala和python的相似度极高,写一下scala时间方面的处理;
1、一个时间戳怎么转化成yyyyMMddHH的格式:
import java.text.SimpleDateFormat import java.util.Date import java.util.Calendar import java.lang.Long import java.util.TimeZone def transformat(date:String,pattern:String):String ={ val myformat = new SimpleDateFormat(pattern) myformat.setTimeZone(TimeZone.getTimeZone("GMT"+8)) val time=new Date(Long.valueof(date)*1000L) myformat.format(time)}
2、怎么将进行时间加减
def addtime(date:String,num:Int):String ={ val myformat = new SimpleDateFormat("yyyyMMddHH") var dnow = new Date() if(date !=""){ dnow=myformat.parse(date)} var cal = Calendar.getInstance() cal.setTime(dnow) cal.add(Calendar.DAY_OF_MONTH,num) val newday= cal.getTime() myformat.format(newday) }
3、怎么对时间进行周末日判断
ef isweekd(date:String,pattern:String):Int ={ val myformat = new SimpleDateFormat("pattern") var dnow =new Date() if(date !=""){ dnow =myformat.parse(date)} val cal = Calendar.getInstance() cal.setTime(dnow) val week=cal.get(Calendar.DAY_OF_WEEK) if (week ==1 || week==7){0} else{1}}