package com.wellsoon.ocs.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class TimeUtil {
public static void main(String[] args)
{
// System.out.println(addDate(new Date(),1));
// System.out.println("getNowDate="+getNowTimeStamp());
System.out.println(getTimeDifference("2008-09-05 17:54:13"));
// System.out.println("aa"+convertDate("2006-11-22","yyyyMMdd")); // System.out.println("tt"+java.sql.Date.valueOf("2006-11-22"));
// System.out.println((float)605040/683501927);
// Date d = new Date();
// System.out.println("0000000000000--------------------------------------------+"+TimeUtil.parseToDate("2010-04-12","yyyy-mm-dd"));
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
// int month = calendar.get(Calendar.MONTH )+1;
int date = calendar.get(Calendar.DATE );
// calendar.set(Calendar.MONTH, -1);
int month = calendar.get(Calendar.MONTH );
// System.out.println(year + " 年 " + month + " 月"+date);
String startTimeTemp = year+"-"+month+"-"+"1";
String endTimeTemp = year+"-"+(month+1)+"-"+"1";
System.out.println(startTimeTemp);
System.out.println(endTimeTemp);
}
public static long getTimeDifference(String time,String time1) {
long between = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String systemTime = sdf.format(new Date()).toString();
Date end = null;
Date begin = null;
try {// 将截取到的时间字符串转化为时间格式的字符串
end = sdf.parse(time);
begin = sdf.parse(systemTime);
} catch (Exception e) {
e.printStackTrace();
}
between = Math.abs(end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
return between;
}
public static String formatDate(Date date) {
try{
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE)).format(date);
}
catch(Exception ex)
{
return "";
}
}
public static String formatDate(Timestamp timestamp) {
try{
Date date=new Date(timestamp.getTime());
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE)).format(date);
}
catch(Exception ex)
{
return "";
}
}
public static String formatTimestamp(Timestamp timestamp) {
try{
Date date=new Date(timestamp.getTime());
return (new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINESE)).format(date);
}
catch(Exception ex)
{
return "";
}
}
public static Date getNowDate() {
return new Date();
}
public static Timestamp getNowTimeStamp() {
return new Timestamp(getNowDate().getTime());
}
public static Timestamp getTimeStamp(long offsetSecond) {
return new Timestamp(getNowDate().getTime()+offsetSecond*1000);
}
public static String getNowDateString() {
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE)).format(new Date());
}
public static String getNowYearString() {
return (new SimpleDateFormat("yyyy", Locale.CHINESE)).format(new Date());
}
public static String getMoth(){
return (new SimpleDateFormat("MM",Locale.CANADA)).format(new Date());
}
public static String parseToString(Date date) {
if (date == null)
return "";
return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
}
public static String parseTotime(Date date) {
if (date == null)
return "";
return (new SimpleDateFormat("HH:mm")).format(date);
}
public static String getshijian(Date date){
if(date==null)
return "";
return (new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));
}
public static String parseToString(Date date, String dateFormatString) {
if (date == null)
return "";
return (new SimpleDateFormat(dateFormatString)).format(date);
}
public static Date parseToDate(String dateString, String dateFormatString) {
try {
if(dateString==null||dateString.equals(""))
return null;
DateFormat dateFormat = new SimpleDateFormat(dateFormatString, Locale.CHINA);
dateFormat.setLenient(false);
return dateFormat.parse(dateString);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static Date parseToDate(String sDateStr) {
return parseToDate(sDateStr,"yyyy-MM-dd");
}
public static Date parseToDate1(String sDateStr) {
return parseToDate(sDateStr,"yyyy-MM-dd HH:mm");
}
public static Date parseToDate3(String sDateStr) {
return parseToDate(sDateStr,"HH:mm");
}
public static Date strToDate(String strDate){
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(strDate);
}catch (Exception e){
System.out.println(e.getMessage());
}
return date;
}
public static Date convertDate(String adateStrteStr, String format) {
java.util.Date date = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
date = simpleDateFormat.parse(adateStrteStr);
} catch (Exception ex) {
ex.printStackTrace();
}
return date;
}
public static String yesterDay() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = new Date();
long myTime = myDate.getTime() / 1000L - 0x15180L;
myDate.setTime(myTime * 1000L);
String yDate = formatter.format(myDate);
return yDate;
}
public static String Tommorrow() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = new Date();
long myTime = myDate.getTime() / 1000L + 0x15180L;
myDate.setTime(myTime * 1000L);
String yDate = formatter.format(myDate);
return yDate;
}
public static Date NextYear(Date date) {
return addDate(date,365);
}
public static Date NextMonth(Date date) {
return addDate(date,31);
}
public static boolean isValidDateStr(String sDateStr, String sDateFormat) {
SimpleDateFormat formatter = new SimpleDateFormat(sDateFormat);
formatter.setLenient(false);
try {
formatter.format(formatter.parse(sDateStr));
} catch (Exception e) {
return false;
}
return true;
}
public static int getSecondLength(String endTime,String startTime)
{
String[] endTimeArray=FormatUtil.stringToArray(endTime,":");
String[] startTimeArray=FormatUtil.stringToArray(startTime,":");
int recordLength=(FormatUtil.toInt(endTimeArray[0],0)-FormatUtil.toInt(startTimeArray[0],0))*3600;
recordLength=recordLength+(FormatUtil.toInt(endTimeArray[1],0)-FormatUtil.toInt(startTimeArray[1],0))*60;
recordLength=recordLength+FormatUtil.toInt(endTimeArray[2],0)-FormatUtil.toInt(startTimeArray[2],0);
return recordLength;
}
public static Date addDate(Date date,int addDays)
{
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,addDays);
return calendar.getTime();
}
public static synchronized String createtNumber() {
java.util.Date dt = new java.util.Date(System.currentTimeMillis());
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String number= fmt.format(dt);
//extension, you can change it.
return number;
}
/**
* 获取当前日期是星期几<br>
*
* @param dt
* @return 当前日期是星期几
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
}
java常用函数代码
最新推荐文章于 2024-10-09 11:20:29 发布