import java.util.Date
import java.text.SimpleDateFormat
import java.util.Calendar
object DateUtil {
//获取今天日期
def getNowDate(): String = {
var now: Date = new Date()
var dateFormat = new SimpleDateFormat("yyyy-MM-dd")
var day = dateFormat.format(now)
day
}
//获取昨天的日期
def getYesterday(): String = {
var dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
var cal: Calendar = Calendar.getInstance()
cal.add(Calendar.DATE, -5)
var yesterday = dateFormat.format(cal.getTime())
yesterday
}
//获取本周开始日期
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)
//获取本周一的日期
period = df.format(cal.getTime())
period
}
//获取本周末的时间
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); //这种输出的是上个星期周日的日期,因为老外把周日当成第一天
cal.add(Calendar.WEEK_OF_YEAR, 1) // 增加一个星期,才是我们中国人的本周日的日期
period = df.format(cal.getTime())
period
}
//本月的第一天
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()) //本月第一天
period
}
//本月的最后一天
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()) //本月最后一天
period
}
//将时间戳转化成日期 时间戳是秒数,需要乘以1000l转化成毫秒
def DateFormat(time: String): String = {
var sdf: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
var date: String = sdf.format(new Date((time.toLong * 1000l)))
date
}
//时间戳转化为时间,原理同上
def timeFormat(time: String): String = {
var sdf: SimpleDateFormat = new SimpleDateFormat("HH:mm:ss")
var date: String = sdf.format(new Date((time.toLong * 1000l)))
date
}
def fun(data1: String, date2: String) {
val sdf = new SimpleDateFormat("yyyy-mm-dd")
val b = sdf.parse(data1).getTime - sdf.parse(date2).getTime
val num = b / (1000 * 3600 * 24)
println(num)
}
def countMonths(date1: String, date2: String) {
val sdf = new SimpleDateFormat("yyyy-mm-dd");
val f = 0.0f;
var c1 = Calendar.getInstance();
var c2 = Calendar.getInstance();
c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));
var year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
//println(year)
if (year < 0) {
year == -year
return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
}
return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
}
def months_between(beginDate: String, endDate: String) {
val sdf = new SimpleDateFormat("yyyy-mm-dd");
val begin = sdf.parse(beginDate);
val end = sdf.parse(endDate);
val months = (end.getYear() - begin.getYear()) * 12 + (end.getMonth() - begin.getMonth());
val y = (end.getYear() - begin.getYear()) * 12
val m = end.getMonth() - begin.getMonth();
println(y)
println("=================")
println(m)
}
def getMonthSpace(date1: String, date2: String) {
var result = 0;
var sdf = new SimpleDateFormat("yyyy-MM-dd");
var c1 = Calendar.getInstance();
var c2 = Calendar.getInstance();
c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));
result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
//if (result == 0) 1 else result
if (result == 0) 1 else Math.abs(result)
println(result)
}
def main(args: Array[String]) {
val s = getMonthSpace("2012-05-25","2012-05-27")
println(s)
}
}
网友评论