Because making a time-attendance system, you need to use Scala for the processing of dates and times, including
Get today's date, yesterday's date, this week time, this month time, time stamp conversion Date Time comparison calculation timing, and so on, hereby summarizes.
1. Get today's date
Def getnowdate (): string={
var now:date = new Date ()
var dateformat:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd")
var hehe = Dateformat.format (now)
hehe
}
2, get yesterday's date
Def getyesterday (): string={
var dateformat:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd")
var Cal:calendar=calendar.getinstance ()
cal.add (calendar.date,-1)
var yesterday=dateformat.format ( Cal.gettime ())
yesterday
3. Get the start date of the week
Def getnowweekstart (): string={
var period:string= ""
var Cal:calendar =calendar.getinstance ();
var df:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd");
Cal.set (Calendar.day_of_week, Calendar.monday)
//Get this Monday date
Period=df.format (Cal.gettime ())
period
}
4. Get the time of the week
Def getnowweekend (): string={
var period:string= ""
var Cal:calendar =calendar.getinstance ();
var df:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd");
Cal.set (Calendar.day_of_week, calendar.sunday);//This output is the date of Sunday of last week, because foreigners regard Sunday as the first day
Cal.add (calendar.week_of_ Year, 1)//Add one weeks, is the date of our Chinese this Sunday
Period=df.format (Cal.gettime ())
period
}
5, the first day of the month
Def getnowmonthstart (): string={
var period:string= ""
var Cal:calendar =calendar.getinstance ();
var df:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd");
Cal.set (calendar.date, 1)
Period=df.format (Cal.gettime ())//first day of the month
period
}
6, the last day of this month
Def getnowmonthend (): string={
var period:string= ""
var Cal:calendar =calendar.getinstance ();
var df:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd");
Cal.set (calendar.date, 1)
Cal.roll (calendar.date,-1)
Period=df.format (Cal.gettime ())//Last day
of the month Period
}
7. Convert Timestamps to Dates
Timestamp is the number of seconds that need to be multiplied by 1000l into milliseconds
def dateformat (time:string): string={
var sdf:simpledateformat = new SimpleDateFormat ("Yyyy-mm-dd")
var date:string = Sdf.format (new date ((time.tolong*1000l)))
Date
}
8, time stamp conversion to time, the principle of ibid.
def timeformat (time:string): string={
var sdf:simpledateformat = new SimpleDateFormat ("HH:mm:ss")
var date: String = Sdf.format (new date ((time.tolong*1000l)))
Date
}
9. Test it.
def main (args:array[string]) {
print ("Now Time:" +tool.getnowdate ())
print ("Yesterday Time:" +tool.getyesterday ())
Print ("Start this Week" +tool.getnowweekstart ()) print ("
end of Week" +tool.getnowweekend ())
print ("Start this month" + Tool.getnowmonthstart ()) Print (
"End of Month" +tool.getnowmonthend ()) print (
"\ n") print (
Tool.timeformat ( "1436457603"))
print (Tool.dateformat ("1436457603"))
}
10. Calculating the time difference
/ / Core work time, late to leave early, etc.
Def getCoreTime(start_time:String,end_Time:String)={
Var df:SimpleDateFormat=new SimpleDateFormat("HH:mm:ss")
Var begin:Date=df.parse(start_time)
Var end:Date = df.parse(end_Time)
Var between:Long=(end.getTime()-begin.getTime())/1000//converted to seconds
Var hour:Float=between.toFloat/3600
Var decf:DecimalFormat=new DecimalFormat("#.00")
Decf.format(hour)//format
}