版权声明:wslixiaoliang https://blog.csdn.net/wslixiaoliang/article/details/82227492
代码示例:
package com.cmos.ngkm.web.controller.component.excercise20180601;
import com.cmos.core.logger.Logger;
import com.cmos.core.logger.LoggerFactory;
import com.cmos.ngkm.util.DateUtil;
import com.cmos.ngkm.util.StringUtil;
import java.sql.Timestamp;
/**
* 时间类型格式化(一)
* 格式化为:YYYY-MM-DD hh:mm:ss 日期时间类型
* 传入:日期类型、日期时间类型皆可
*/
public class FormatTime
{
private static Logger logger = LoggerFactory.getApplicationLog(FormatTime.class);
/**
* 获取标准日期时间格式 YYYY-MM-DD hh:mm:ss
* @param dateTime
* @param inStr
* @return
*/
public static Object getFormatTime(Object dateTime, String inStr)
{
//为空判断:参数值为空,直接返回本身(本身即为空)
if (dateTime == null || StringUtil.isEmpty(String.valueOf(dateTime)))
{
return dateTime;
}
String resultValue = "";
String[] dateTimes = String.valueOf(dateTime).split(" ");
String dates[] = dateTimes[0].split("-");
if (dates == null || dates.length == 0) {
return dateTime;
}
//遍历年月日(年月日都不为数字,则赋给其默认值并返回)
for (int i = 0; i < dates.length; i++)
{
if (!StringUtil.isNum(dates[i]))
{
String str;
if (inStr.indexOf("start") > -1)
{
str = "2010-01-01 00:00:00";
}
else
{
str = "2099-01-01 23:59:59";
}
return str;
}
}
// 补全:年月日
if (dates.length == 1)//1只有年 2018
{
resultValue += dates[0];//年月日,一直往后拼接
}
else if (dates.length == 2)//只有年月
{
resultValue += dates[0];//加上年
if (dates[1].length() == 1)
{
resultValue = resultValue + "-0" + dates[1] + "-01";//2018-06-01 日:默认给01
}
else
{
resultValue = resultValue + "-" + dates[1] + "-01";//2018-06-01 日:默认给01
}
}
else if (dates.length == 3) //有年月日
{
resultValue += dates[0];
if (dates[1].length() == 1) {
resultValue = resultValue + "-0" + dates[1];
} else {
resultValue = resultValue + "-" + dates[1];
}
if (dates[2].length() == 1) {
resultValue = resultValue + "-0" + dates[2];
} else {
resultValue = resultValue + "-" + dates[2];
}
}
//如果:时间字段值为:年月日+时分秒 且值不为数字,则给默认值
if (dateTimes.length == 2)
{
String times[] = dateTimes[1].split(":");
for (int i = 0; i < times.length; i++)
{
String str;
if (!StringUtil.isNum(times[i])) //如果年月日不为数字,则给默认值
{
if (inStr.indexOf("start") > -1)
{
str = "2010-01-01 00:00:00";
}
else
{
str = "2099-01-01 23:59:59";
}
return str;
}
}
// 补全:时分秒
if (times.length == 1)//只有时
{
if (times[0].length() == 1)
{
resultValue = resultValue + " 0" + times[0] + ":00:00";//小时为1位:如:5;拼接后 05:00:00
}
else
{
resultValue = resultValue + " " + times[0] + ":00:00";//小时为2位:如:05;拼接后 05:00:00
}
}
else if (times.length == 2)//只有时分
{
if (times[0].length() == 1)
{
resultValue = resultValue + " 0" + times[0];//小时为1位:如:5;拼接后 05:00:00(times[0]:即为小时的值)
}
else
{
resultValue = resultValue + " " + times[0];//小时为2位:如:5;拼接后 05:00:00(times[0]:即为小时的值)
}
if (times[1].length() == 1)
{
resultValue = resultValue + ":0" + times[1] + ":00";//分为1位:如:5;拼接后 05:05:00(times[1]:即为分的值)
}
else
{
resultValue = resultValue + ":" + times[1] + ":00";
}
}
else if (times.length == 3) ////时分秒都有
{
if (times[0].length() == 1)
{
resultValue = resultValue + " 0" + times[0];
}
else
{
resultValue = resultValue + " " + times[0];
}
if (times[1].length() == 1)
{
resultValue = resultValue + ":0" + times[1];
}
else
{
resultValue = resultValue + ":" + times[1];
}
if (times[2].length() == 1)
{
resultValue = resultValue + ":0" + times[2];
}
else
{
resultValue = resultValue + ":" + times[2];
}
}
}
else //如果长度为1 (只有年月日,没有时分秒)
{
if(inStr.indexOf("start") > -1)
{
resultValue = resultValue + " 00:00:00";
}
else
{
resultValue = resultValue + " 23:59:59";
}
}
String str;
try
{
str = DateUtil.date2String(Timestamp.valueOf(resultValue), DateUtil.DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);//格式化时间格式
}
catch (Exception e)
{
if (inStr.indexOf("start") > -1)
{
str = "2010-01-01 00:00:00";
}
else
{
str = "2099-01-01 23:59:59";
}
logger.info(e.getMessage(), e);
return str;
}
return str;
}
/**
* 时间类型格式化(二)
* 格式化为:YYYY-MM-DD 日期类型
* 传入:日期类型、日期时间类型皆可
*/
public static Object getDateTime(Object dateTime, String inStr)
{
//为空判断:参数值为空,直接返回本身(本身即为空)
if (dateTime == null || StringUtil.isEmpty(String.valueOf(dateTime)))
{
return dateTime;
}
String resultValue = "";
String[] dateTimes = String.valueOf(dateTime).split(" ");
String dates[] = dateTimes[0].split("-");
if (dates == null || dates.length == 0) {
return dateTime;
}
//遍历年月日(年月日都不为数字,则赋给其默认值并返回)
for (int i = 0; i < dates.length; i++)
{
if (!StringUtil.isNum(dates[i]))
{
String str;
if (inStr.indexOf("start") > -1)
{
str = "2010-01-01 00:00:00";
}
else
{
str = "2099-01-01 23:59:59";
}
return str;
}
}
// 补全:年月日
if (dates.length == 1)//1只有年 2018
{
resultValue += dates[0];//年月日,一直往后拼接
}
else if (dates.length == 2)//只有年月
{
resultValue += dates[0];//加上年
if (dates[1].length() == 1)
{
resultValue = resultValue + "-0" + dates[1] + "-01";//2018-06-01 日:默认给01
}
else
{
resultValue = resultValue + "-" + dates[1] + "-01";//2018-06-01 日:默认给01
}
}
else if (dates.length == 3) //有年月日
{
resultValue += dates[0];
if (dates[1].length() == 1) {
resultValue = resultValue + "-0" + dates[1];
} else {
resultValue = resultValue + "-" + dates[1];
}
if (dates[2].length() == 1) {
resultValue = resultValue + "-0" + dates[2];
} else {
resultValue = resultValue + "-" + dates[2];
}
}
//如果:时间字段值为:年月日+时分秒 且值不为数字,则给默认值
if (dateTimes.length == 2)
{
String times[] = dateTimes[1].split(":");
for (int i = 0; i < times.length; i++)
{
String str;
if (!StringUtil.isNum(times[i])) //如果年月日不为数字,则给默认值
{
if (inStr.indexOf("start") > -1)
{
str = "2010-01-01 00:00:00";
}
else
{
str = "2099-01-01 23:59:59";
}
return str;
}
}
// 补全:时分秒
if (times.length == 1)//只有时
{
if (times[0].length() == 1)
{
resultValue = resultValue + " 0" + times[0] + ":00:00";//小时为1位:如:5;拼接后 05:00:00
}
else
{
resultValue = resultValue + " " + times[0] + ":00:00";//小时为2位:如:05;拼接后 05:00:00
}
}
else if (times.length == 2)//只有时分
{
if (times[0].length() == 1)
{
resultValue = resultValue + " 0" + times[0];//小时为1位:如:5;拼接后 05:00:00(times[0]:即为小时的值)
}
else
{
resultValue = resultValue + " " + times[0];//小时为2位:如:5;拼接后 05:00:00(times[0]:即为小时的值)
}
if (times[1].length() == 1)
{
resultValue = resultValue + ":0" + times[1] + ":00";//分为1位:如:5;拼接后 05:05:00(times[1]:即为分的值)
}
else
{
resultValue = resultValue + ":" + times[1] + ":00";
}
}
else if (times.length == 3) ////时分秒都有
{
if (times[0].length() == 1)
{
resultValue = resultValue + " 0" + times[0];
}
else
{
resultValue = resultValue + " " + times[0];
}
if (times[1].length() == 1)
{
resultValue = resultValue + ":0" + times[1];
}
else
{
resultValue = resultValue + ":" + times[1];
}
if (times[2].length() == 1)
{
resultValue = resultValue + ":0" + times[2];
}
else
{
resultValue = resultValue + ":" + times[2];
}
}
}
else //如果长度为1 (只有年月日,没有时分秒)
{
if(inStr.indexOf("efft") > -1)
{
resultValue = resultValue + " 00:00:00";
}
else
{
resultValue = resultValue + " 23:59:59";
}
}
String str;
try
{
str = DateUtil.date2String(Timestamp.valueOf(resultValue), DateUtil.DATE_PATTERN.YYYY_MM_DD);//格式化时间格式
}
catch (Exception e)
{
if (inStr.indexOf("efft") > -1)
{
str = "2010-01-01";
}
else
{
str = "2099-01-01";
}
logger.info(e.getMessage(), e);
return str;
}
return str;
}
/**
* 测试方法
* @param args
*/
public static void main (String[] args)
{
String dateTime1 = "2018-06-01";
String dateTime2 = "2018-06-03";
String inputStr1 = "start";
String inputStr2 = "end";
String dateTime3 = "2018-06-01 00:00:00";
String dateTime4 = "2018-06-03 23:59:59";
String inputStr3 = "efft";
String inputStr4 = "invild";
logger.info("===============start==================="+getFormatTime(dateTime3,inputStr1));
logger.info("===============end==================="+getFormatTime(dateTime4,inputStr2));
logger.info("===============efft==================="+getDateTime(dateTime3,inputStr3));
logger.info("===============invild==================="+getDateTime(dateTime4,inputStr4));
}
}