package com.MC.utils
import java.text.SimpleDateFormat
import java.util.regex.Pattern
import scala.util.control.Breaks._
import java.math.BigDecimal
import java.util.Calendar
import com.MC.enum.DateEnum
import scala.math.BigDecimal.RoundingMode
/**
* Created by MC on 2018/8/10.
*/
object Utils {
// def main(args: Array[String]): Unit = {
// println(getDateInfo(System.currentTimeMillis(),DateEnum.day))
// }
/**
* 返回指定的时间单位,例如:year,season,month,week,day
*
* @param longTime
* @param dateType
*/
def getDateInfo(longTime: Long, dateType: DateEnum.Value) = {
val calendar = Calendar.getInstance()
calendar.setTimeInMillis(longTime)
if (dateType.equals(DateEnum.year)) {
calendar.get(Calendar.YEAR)
} else if (dateType.equals(DateEnum.season)) {
//month==>0-11
val month = calendar.get(Calendar.MONTH) + 1
if (month % 3 == 0) {
month / 3
} else {
month / 3 + 1
}
} else if (dateType.equals(DateEnum.month)) {
calendar.get(Calendar.MONTH) + 1
} else if (dateType.equals(DateEnum.week)) {
val month = calendar.get(Calendar.MONTH) + 1
var week = calendar.get(Calendar.WEEK_OF_YEAR)
if (month == 12 && week == 1) {
week = 53
}
week
} else if (dateType.equals(DateEnum.day)) {
calendar.get(Calendar.DAY_OF_MONTH)
} else {
throw new Exception(
"""
|Usage:com.daoke360.utils.Utils
|在getDateInfo方法中,dateType参数传入有误
""".stripMargin)
}
}
/**
* 四舍五入函数
*
* @param doubleValue
* @param scale
* @return
*/
def getScale(doubleValue: Double, scale: Int) = {
val bigDecimal = new BigDecimal(doubleValue)
bigDecimal.setScale(scale, RoundingMode.HALF_UP).doubleValue()
}
/**
* 获取字符串中某个字段的值
*
* @param value 原始字符串 session_count=0|1s_3s=7|4s_6s=0|7s_9s=0|10s_30s=0|30s_60s=0|1m_3m=0|3m_10m=0|10m_30m=0|30m=0|1_3=0|4_6=0|7_9=0|10_30=0|30_60=0|60=0
* @param fieldName 字段名称,例如:session_count/1s_3s/4s_6s/....
* @param separator 字符串分隔符 "|"
*/
def getFieldValue(value: String, fieldName: String, separator: String) = {
var fieldValue: String = null
//items===>Array(session_count=0,1s_3s=7,4s_6s=0,....)
val items = value.split(separator)
breakable({
for (item <- items) {
//kv==>Array(session_count,0)
val kv = item.split("=")
if (kv(0).equals(fieldName)) {
fieldValue = kv(1)
break()
}
}
})
fieldValue
}
/**
* 设置某个字段的值
*
* @param value 原始字符串 session_count=0|1s_3s=7|4s_6s=0|7s_9s=0|10s_30s=0|30s_60s=0|1m_3m=0|3m_10m=0|10m_30m=0|30m=0|1_3=0|4_6=0|7_9=0|10_30=0|30_60=0|60=0
* @param fieldName session_count
* @param fieldNewValue 7
* @param separator |
* @return session_count=7|1s_3s=7|4s_6s=0|7s_9s=0|10s_30s=0|30s_60s=0|1m_3m=0|3m_10m=0|10m_30m=0|30m=0|1_3=0|4_6=0|7_9=0|10_30=0|30_60=0|60=0
*/
def setFieldValue(value: String, fieldName: String, fieldNewValue: String, separator: String) = {
var fieldValue: String = null
//items===>Array(session_count=7,1s_3s=7,4s_6s=0,....)
val items = value.split(separator)
breakable({
for (i <- 0 until (items.length)) {
val item = items(i)
val kv = item.split("=")
if (kv(0).equals(fieldName)) {
items(i) = fieldName + "=" + fieldNewValue
break()
}
}
})
items.mkString("|")
}
/**
* 验证一个字符串是否是数字
*/
def validateIsNumber(stringNum: String) = {
val regex = "\\d+"
val pattern = Pattern.compile(regex)
pattern.matcher(stringNum).matches()
}
/**
* 将时间戳转换成任意格式的是时间
*
* @param longTime
* @param pattern
*/
def formatDate(longTime: Long, pattern: String) = {
val simpleDateFormat = new SimpleDateFormat(pattern)
val calendar = simpleDateFormat.getCalendar
calendar.setTimeInMillis(longTime)
simpleDateFormat.format(calendar.getTime)
}
/**
* 将指定格式的日期转换成时间戳
*
* @param inputDate
* @param pattern
*/
def parseDate(inputDate: String, pattern: String) = {
val simpleDateFormat = new SimpleDateFormat(pattern)
val date = simpleDateFormat.parse(inputDate)
date.getTime
}
/**
* 验证输入的日期是否是指定的格式:yyyy-MM-dd
*
* @param inputDate
* @return
*/
def validateInputDate(inputDate: String) = {
val regex = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)"
val pattern = Pattern.compile(regex)
pattern.matcher(inputDate).matches()
}
/**
* 将ip解析成数字
* [.] *
*
* @param ip
* @return
*/
def ipToLong(ip: String): Long = {
val fields = ip.split("[.]")
var numIP: Long = 0
for (i <- 0 until (fields.length)) {
numIP = numIP << 8 | fields(i).toLong
}
numIP
}
/**
* 将nginx服务器器时间转换成时间戳
*
* @param nginxServerTime 1522284413.802:String===> 1522284413802:Long
*/
def nginxServerTimeToLong(nginxServerTime: String) = {
(nginxServerTime.toDouble * 1000).toLong
}
}