【转】scala时间、时间戳系列操作格式说明:
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
java时间日期格式转换a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
格式⽰例:
Date and Time Pattern Result
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010*********-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" 2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u" 2001-W27-3
"⾃由输⼊YYYY-'爱你哦'ww-u⾃由输⼊" ⾃由输⼊2001-爱你哦27-3⾃由输⼊
SimpleDateFormat
import java.util.{Calendar, Date}
/**
* 获取当前时间
*
* @param pattern pattern 如"yyyyMMddHHmmss"
* @return
*/
def getrealTime(pattern: String): String = {
val timeTag = System.currentTimeMillis()
val changeTime = new Date(timeTag)
val dataFormat = new SimpleDateFormat(pattern)
dataFormat.format(changeTime)
}
/**
* 获取当前时间戳(精确到毫秒)
*
* @return
*/
def getTimestamp(): Long = {
val time = alTime("yyyyMMddHHmmss")
Util.funStringToTimeStamp(time, "yyyyMMddHHmmss")
}
/**
* 将时间字符串修改为时间戳
*
* @param time 时间
* @param timeFormatted 时间格式如 "yyyyMMddHHmmss"
* @return 精确到毫秒的时间戳
*/
def funStringToTimeStamp(time: String, timeFormatted: String): Long = {
val fm = new SimpleDateFormat(timeFormatted)
val dt = fm.parse(time)
}
/**
* 将时间戳转换为时间
*
* @param timestamp 精确到毫秒
* @param timeFormatted 时间格式如“HH”表⽰按24⼩时制返回时间戳对应的⼩时
* @return
*/
def timestampToString(timestamp: String, timeFormatted: String): String = {
val fm = new SimpleDateFormat(timeFormatted)
val time = fm.format(new Long))
time
}
/**
* 获取传⼊⽇期与当前⽇期的天数差
*
* @param day 待⽐较⽇期(仅包含⽉⽇)
* @param dayFormatted 待⽐较⽇期格式(例如:MMdd或MM-dd)
* @return
*/
def daysBetweenToday(day: String, dayFormatted: String): Int = {
val calendar = Instance
calendar.setTime(new Date())
val today = (Calendar.DAY_OF_YEAR)
val year = (Calendar.YEAR)
val inputF = new SimpleDateFormat("yyyy" + dayFormatted)
val date = inputF.parse(year + day)
calendar.setTime(date)
val inputDay = (Calendar.DAY_OF_YEAR)
var days = today - inputDay
if (days < 0) {
val beforeYearDate = inputF.parse((year - 1) + day)
calendar.setTime(beforeYearDate)
days = ActualMaximum(Calendar.DAY_OF_YEAR) - (Calendar.DAY_OF_YEAR) + today }
return days
}
/**
* 将毫秒级时间戳转化成分钟级时间戳
*
* @param time 毫秒级时间戳
* @return 分钟级时间戳
*/
def getMinTimestamp(time: Long): Long = {
val minTime = time / (1000 * 60)
minTime
}
/**
* 将时间字符串修改为格式
*
* @param inpuTime 输⼊时间
* @param inputFormatted 输⼊时间格式
* @param outputFormatted 输出时间格式
* @return
*/
def formatTime(inpuTime: String, inputFormatted: String, outputFormatted: String): String = {
val inputF = new SimpleDateFormat(inputFormatted)
val outputF = new SimpleDateFormat(outputFormatted)
val inputT = inputF.parse(inpuTime)
outputF.format(inputT)
}
/
**
* 获取传⼊时间戳的天数差
*
* @param t1 较⼩时间戳
* @param t2 较⼤时间戳
* @return
*/
def caculate2Days(t1: Long, t2: Long): Int = {
import java.util.Calendar
val calendar = Instance
calendar.setTimeInMillis(t2)
val t2Day = (Calendar.DAY_OF_YEAR)
calendar.setTimeInMillis(t1)
val t1Day = (Calendar.DAY_OF_YEAR)
var days = t2Day - t1Day
if (days < 0) {
days = ActualMaximum(Calendar.DAY_OF_YEAR) - t1Day + t2Day
}
return days;
}
/**
* 判断nowTime是否在startTime与endTime之间
* @param nowTime
* @param startTime
* @param endTime
* @param formater
* @return
*/
def isBetweenDate(nowTime: String, startTime: String, endTime: String, formater: String): Boolean = { val df = new SimpleDateFormat(formater)
val nowDate = df.parse(nowTime)
val startDate = df.parse(startTime)
val endDate = df.parse(endTime)
if ((Time == Time) || (Time == Time)) return true if (nowDate.after(startDate) && nowDate.before(endDate)) {
true
} else {
false
}
}
/**
* 时间增加天数,返回时间
*
* @param date ⼊参时间
* @param num 增加的天数
* @return
*/
def funAddDate(date: String, num: Int): String = {
val myformat = new SimpleDateFormat("yyyyMMdd")
var dnow = new Date()
dnow = myformat.parse(date)
val cal = Instance()
cal.setTime(dnow)
cal.add(Calendar.DAY_OF_MONTH, num)
val newday = Time
myformat.format(newday)
}
/**
* 时间增加⼩时,返回时间
*
* @param date ⼊参时间
* @param num 增加的天数
* @return
*/
def funAddHour(date: String, num: Int): String = {
val myformat = new SimpleDateFormat("yyyyMMddHH")
var dnow = new Date()
dnow = myformat.parse(date)
val cal = Instance()
cal.setTime(dnow)
cal.add(Calendar.HOUR, num)
val newday = Time
myformat.format(newday)
}
/**
* 时间增加分钟,返回时间
*
* @param date ⼊参时间
* @param num 增加的分钟数
* @return
*/
def funAddMinute(date: String, num: Int): String = {
val myformat = new SimpleDateFormat("yyyyMMddHHmm")
var dnow = new Date()
dnow = myformat.parse(date)
val cal = Instance()
cal.setTime(dnow)
cal.add(Calendar.MINUTE, num)
val newday = Time
myformat.format(newday)
}
/**
* 时间增加秒,返回时间
*
* @param date ⼊参时间
* @param num 增加的秒数
* @return
*/
def funAddSecond(date: String, num: Int, format: String): String = { val myformat = new SimpleDateFormat(format)
var dnow = new Date()
dnow = myformat.parse(date)
val cal = Instance()
cal.setTime(dnow)
cal.add(Calendar.SECOND, num)
val newday = Time
myformat.format(newday)
}
/** 获取过去⼏天的时间数组
*
* @param date
* @param num
* @return
*/
def getPastDays(date: String, num: Int): Array[String] = {
val buffer = new ArrayBuffer[String]()
val range = 0 until num
for(i <- range){
buffer.append(funAddDate(date,-i))
}
}
/** 获取过去⼏⼩时的时间数组
*
* @param date
* @param num
* @return
*/
def getPastHours(date: String, num: Int, interval:Int): Array[String] = { val buffer = new ArrayBuffer[String]()
val range = 0 until num
for(i <- range){
buffer.append(funAddHour(date,-i*interval))
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论