package com.ofsoft.cms.core.utils;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class DateTimeUtil {
public static final SimpleDateFormat FORMAT_YYYY_MM_DDHHMMSS = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat(
"yyyy-MM-dd");
public static final SimpleDateFormat FORMAT_YYYY_MM = new SimpleDateFormat(
"yyyy-MM");
public static final SimpleDateFormat FORMAT_YYYYMMDDHHMMSS17 = new SimpleDateFormat(
"yyyyMMddHHmmssSSS");
public static final SimpleDateFormat FORMAT_YYYYMMDDHHMMSS14 = new SimpleDateFormat(
"yyyyMMddHHmmss");
public static final SimpleDateFormat FORMAT_YYMMDDHHMMSS12 = new SimpleDateFormat(
"yyMMddHHmmss");
public static final SimpleDateFormat FORMAT_YYYYMMDD8 = new SimpleDateFormat(
"yyyyMMdd");
public static final SimpleDateFormat FORMAT_YYYYMM6 = new SimpleDateFormat(
"yyyyMM");
public static final SimpleDateFormat FORMAT_HHMMSS8 = new SimpleDateFormat(
"HH:mm:ss");
/** 返回当前时间的Date */
public static java.util.Date nowDate() {
return new java.util.Date();
}
/**
* 字符串转为时间,字符串符合标准格式:"YYYY-MM-DD HH:MM:SS"
*
* @param dateTime
* 标准时间格式 "YYYY-MM-DD HH:MM:SS"
* @return java.util.Date
*/
public static java.util.Date toDate(String dateTime) {
if (dateTime.length() > "YYYY-MM-DD HH:MM:SS".length())
dateTime = dateTime.substring(0, "YYYY-MM-DD HH:MM:SS".length());
int index = dateTime.indexOf(" ");
String date = dateTime.substring(0, index);
String time = dateTime.substring(index + 1);
return toDate(date, time);
}
/**
* 字符串转为时间,字符串符合标准日期格式:"YYYY-MM-DD",和标准时间格式:"HH:MM:SS"
*
* @param date
* 标准日期格式 "YYYY-MM-DD"
* @param time
* 标准时间格式 "HH:MM:SS"
* @return java.util.Date
*/
public static java.util.Date toDate(String date, String time) {
if (date == null || time == null)
return null;
int dateSlash1 = date.indexOf("-");
int dateSlash2 = date.lastIndexOf("-");
if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
return null;
int timeColon1 = time.indexOf(":");
int timeColon2 = time.lastIndexOf(":");
if (timeColon1 <= 0 || timeColon1 == timeColon2)
return null;
String year = date.substring(0, dateSlash1);
String month = date.substring(dateSlash1 + 1, dateSlash2);
String day = date.substring(dateSlash2 + 1);
String hour = time.substring(0, timeColon1);
String minute = time.substring(timeColon1 + 1, timeColon2);
String second = time.substring(timeColon2 + 1);
;
return toDate(year, month, day, hour, minute, second);
}
/**
* 通过标准时间输入,年,月,日,时,分,秒,生成java.util.Date
*
* @param yearStr
* 年
* @param monthStr
* 月
* @param dayStr
* 日
* @param hourStr
* 时
* @param minuteStr
* 分
* @param secondStr
* 秒
* @return java.util.Date
*/
public static java.util.Date toDate(String yearStr, String monthStr,
String dayStr, String hourStr, String minuteStr, String secondStr) {
int year, month, day, hour, minute, second;
try {
year = Integer.parseInt(yearStr);
month = Integer.parseInt(monthStr);
day = Integer.parseInt(dayStr);
hour = Integer.parseInt(hourStr);
minute = Integer.parseInt(minuteStr);
second = Integer.parseInt(secondStr);
} catch (Exception e) {
return null;
}
return toDate(year, month, day, hour, minute, second);
}
/**
* 通过标准时间输入,年,月,日,时,分,秒,生成java.util.Date
*
* @param year
* 年
* @param month
* 月
* @param day
* 日
* @param hour
* 时
* @param minute
* 分
* @param second
* 秒
* @return java.util.Date
*/
public static java.util.Date toDate(int year, int month, int day, int hour,
int minute, int second) {
Calendar calendar = Calendar.getInstance();
try {
calendar.set(year, month - 1, day, hour, minute, second);
} catch (Exception e) {
return null;
}
return calendar.getTime();
}
/**
* 生成标准格式的字符串 格式为: "MM-DD-YYYY HH:MM:SS"
*
* @param date
* The Date
* @return 生成默认格式的字符串 格式为: "MM-DD-YYYY HH:MM:SS"
*/
public static String toDateTimeString(java.util.Date date) {
if (date == null)
return "";
String dateString = toDateString(date);
String timeString = toTimeString(date);
if (dateString == null || timeString == null)
return "";
return dateString + " " + timeString;
}
/**
* 生成标准日期,格式为 YYYY+spe+MM+spe+DD
*
* @param date
* The Date
* @return 生成日期,格式为 YYYY+spe+MM+spe+DD
*/
public static String toDateString(java.util.Date date, String spe) {
if (date == null)
return "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int year = calendar.get(Calendar.YEAR);
String monthStr = "" + month;
String dayStr = "" + day;
String yearStr = "" + year;
if (month < 10)
monthStr = "0" + month;
if (day < 10)
dayStr = "0" + day;
return yearStr + spe + monthStr + spe + dayStr;
}
/**
* 生成标准日期,格式为 YYYY-MM-DD
*
* @param date
* The Date
* @return 生成日期,格式为 YYYY-MM-DD
*/
public static String toDateString(java.util.Date date) {
return toDateString(date, "-");
}
/**
* 根据输入的时间,生成时间格式 HH:MM:SS
*
* @param date
* java.util.Date
* @return 生成时间格式为 HH:MM:SS
*/
public static String toTimeString(java.util.Date date) {
if (date == null)
return "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return toTimeString(calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
}
/**
* 根据输入的时,分,秒,生成时间格式 HH:MM:SS
*
* @param hour
* 时
* @param minute
* 分
* @param second
* 秒
* @return 生成时间格式为 HH:MM:SS
*/
public static String toTimeString(int hour, int minute, int second) {
String hourStr = "" + hour;
String minuteStr = "" + minute;
String secondStr = "" + second;
if (hour < 10)
hourStr = "0" + hour;
if (minute < 10)
minuteStr = "0" + minute;
if (second < 10)
secondStr = "0" + second;
return hourStr + ":" + minuteStr + ":" + secondStr;
}
/**
* 取得给定日历,给定格式的日期字符串
*
* @param calendar
* 日历,给定一个日历
* @return String 取得默认的日期时间字符串"yyyy-MM-dd"
*/
public static String toDateString(Calendar calendar) {
return FORMAT_YYYY_MM_DD.format(calendar.getTime());
}
/**
* 取得给定日历,给定格式的日期时间字符串
*
* @param calendar
* 日历,给定一个日历
* @return String 取得默认的日期时间字符串"yyyy-MM-dd HH:mm:ss"
*/
public static String toDateTimeString(Calendar calendar) {
return FORMAT_YYYY_MM_DDHHMMSS.format(calendar.getTime());
}
/**
* 取得给定日历,给定格式的日期时间字符串
*
* @param calendar
* 日历,给定一个日历
* @param format
* 格式,如String format = "yyyy-MM-dd HH:mm:ss";
* @return String 取得给定日历,给定格式的日期时间字符串
*/
public static String toDateTimeString(Calendar calendar, String format) {
return toDateTimeString(calendar.getTime(), format);
}
/**
* 取得给定时间,给定格式的日期时间字符串,标准格式:"yyyy-MM-dd
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ofcms内容管理系统 v1.1.4.zip (1283个子文件)
comn.sql.html.bak 2KB
layui.css 52KB
layui.css 52KB
layui.css 52KB
layui.css 52KB
ueditor.css 43KB
font-awesome.min.css 35KB
ueditor.min.css 34KB
video-js.css 21KB
image.css 18KB
swiper.min.css 17KB
layim.css 15KB
layim.css 15KB
layim.css 15KB
layim.css 15KB
video.css 15KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
attachment.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
video-js.min.css 11KB
layui.mobile.css 10KB
layui.mobile.css 10KB
layui.mobile.css 10KB
layui.mobile.css 10KB
admin2.css 9KB
awesome.css 8KB
laydate.css 7KB
laydate.css 7KB
laydate.css 7KB
laydate.css 7KB
shCoreDefault.css 7KB
metroStyle.css 6KB
zTreeStyle.css 6KB
admin.css 6KB
jquery.contextMenu.min.css 5KB
layer.css 5KB
layer.css 5KB
layer.css 5KB
layer.css 5KB
scrawl.css 4KB
codemirror.css 3KB
login2.css 3KB
index.css 3KB
index.css 3KB
charts.css 3KB
background.css 2KB
product.css 2KB
pace-theme-flash.css 2KB
pace.min.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
edittable.css 1KB
code.css 1KB
code.css 1KB
code.css 1KB
code.css 1KB
template.css 1KB
comment.css 920B
page.css 773B
login.css 766B
webuploader.css 515B
case.css 453B
help.css 389B
product.css 349B
iframe.css 41B
iconfont.eot 37KB
iconfont.eot 37KB
iconfont.eot 37KB
iconfont.eot 37KB
fontawesome-webfont.eot 37KB
vjs.eot 3KB
context-menu-icons.eot 3KB
UEditorSnapscreen.exe 508KB
wface.gif 49KB
jxface2.gif 40KB
yface.gif 28KB
bface.gif 27KB
icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
tface.gif 19KB
fface.gif 18KB
59.gif 10KB
59.gif 10KB
59.gif 10KB
59.gif 10KB
22.gif 10KB
22.gif 10KB
22.gif 10KB
22.gif 10KB
cface.gif 8KB
24.gif 8KB
24.gif 8KB
共 1283 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
芝麻粒儿
- 粉丝: 6w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功