package arithmetic;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 有关日期时间的工具类
* @author liujd
*
*/
public class DataUtil {
//格式化"yyyy-MM-dd"
private static final String Date_Format_day = "yyyy-MM-dd";
//格式化"yyyy-MM-dd HH:mm:ss"
private static final String Date_Format_seconds = "yyyy-MM-dd HH:mm:ss";
//格式化"yyyy-MM-dd HH:mm:ss"
private static final String Date_Format_hms = "HH:mm:ss";
/**
* 格式化"yyyy-MM-dd"
*
* @param Date
* @return String
*/
public static String FormatToDay(Date dateNow) {
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format_day);
return sdf.format(dateNow);
}
/**
* 格式化"yyyy-MM-dd HH:mm:ss"
*
* @param Date
* @return String
*/
public static String FormatToSeconds(Date dateNow) {
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format_seconds);
return sdf.format(dateNow);
}
/**
* 格式化"HH:mm:ss"
*
* @param Date
* @return String
*/
public static String FormatHms(Date dateNow) {
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format_hms);
return sdf.format(dateNow);
}
/**
* 获取年 月 日 时 分 秒
* @param String Date
* @return int
*/
@SuppressWarnings("deprecation")
public static int getByType(Date dateNow,String type) {
int result = 0;
switch (type) {
case "year":
result = dateNow.getYear() + 1900;
break;
case "month":
result = dateNow.getMonth() + 1;
break;
case "day":
result = dateNow.getDate();
break;
case "hours":
result = dateNow.getHours();
break;
case "minutes":
result = dateNow.getMinutes();
break;
case "seconds":
result = dateNow.getSeconds();
break;
default:
result = 0;
break;
}
return result;
}
/**
* 将日期转换为字符串
*
* @param Date
* @return String
*/
@SuppressWarnings("deprecation")
public static String DateToString(Date dateNow) {
return dateNow.toLocaleString();
}
/**
* 将时间格式是 "yyyy-MM-dd HH:mm:ss"的字符串转换为日期
* @param String
* @return Date
*/
public static Date StringToDate1(String dateString) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format_seconds);
return sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 将时间格式是 "yyyy-MM-dd"的字符串转换为日期
* @param String
* @return Date
*/
public static Date StringToDate2(String dateString) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format_day);
return sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取两个日期相差的天数
* @param Date Date
* @return int
*/
public static int diffOfTwo(Date date1 , Date date2) {
long time1 = date1.getTime();
long time2 = date2.getTime();
int diff = (int)((time1-time2)/60*60*24*1000);
return diff;
}
/**
* 指定日期加上天数
* @param Date int
* @return Date
*/
public static Date addDate(Date date , int day) {
Calendar c = Calendar.getInstance();
c.setTime(date);
long timeInMillis = c.getTimeInMillis();
c.setTimeInMillis(timeInMillis+(((long)day)*24*60*60*1000));
return c.getTime();
}
/**
* 指定日期减去天数
* @param Date int
* @return Date
*/
public static Date subDate(Date date , int day) {
Calendar c = Calendar.getInstance();
c.setTime(date);
long timeInMillis = c.getTimeInMillis();
c.setTimeInMillis(timeInMillis-(((long)day)*24*60*60*1000));
return c.getTime();
}
/**
* 获取年
* @param Date
* @return int
*/
public static int getYear(Date date) {
if(date == null) {
date = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.YEAR);
}
/**
* 获取月
* @param Date
* @return int
*/
public static int getMonth(Date date) {
if(date == null) {
date = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MONTH) + 1;
}
/**
* 获取日
* @param Date
* @return int
*/
public static int getDay(Date date) {
if(date == null) {
date = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}
/**
* 获取星期
* @param Date
* @return int
*/
public static int getWeek(Date date) {
if(date == null) {
date = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
if(week == 0) {
return 7;
}else {
return week;
}
}
/**
* 判断字符串是否是时间格式
* 默认时间格式是 "yyyy-MM-dd"
* @param string
* @return boolean
*/
public static boolean isDateFormat(String dateString) {
SimpleDateFormat sdf = new SimpleDateFormat(Date_Format_day);
if(isEmpty(dateString)) {
return false;
}
try {
sdf.parse(dateString);
return true;
} catch (ParseException e) {
e.printStackTrace();
return false;
}
}
/**
* 判断字符串是否为空
* @param string
* @return boolean
*/
public static boolean isEmpty(String str) {
return str.trim().length() == 0 ? true : false;
}
public static void main(String[] args) {
System.out.println(FormatToDay(new Date()));;
System.out.println(FormatToSeconds(new Date()));;
}
}
暂时就想到这些 以后补上