- 简介
有时候项目中会用到时间与时间戳相互转化,直接拷贝到项目中即可使用
/**
* @author : Created by Jack-Chen
* @date: on 2015/10/18.
* @function: 时间转换工具类
*/
public class TimeTransformationUtils {
/**
* 灏嗛暱鏃堕棿鏍煎紡瀛楃涓茶浆鎹负鏃堕棿 yyyy-MM-dd HH:mm:ss
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 获取现在时间
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取现在时间
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static String getStringDateTomorrow() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-(dd+1)");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取现在时间
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取现在时间
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
/**
* 获取时间 小时:分;秒 HH:mm:ss
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date currentTime = new Date();
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
* @param dateDate
* @return
*/
public static String dateToStrLong(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式时间转换为字符串 yyyy-MM-dd
* @param dateDate
* @param k
* @return
*/
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 得到现在时间
* @return
*/
public static Date getNow() {
Date currentTime = new Date();
return currentTime;
}
/**
* 提取一个月中的最后一天
* @param day
* @return
*/
public static Date getLastDate(long day) {
Date date = new Date();
long date_3_hm = date.getTime() - 3600000 * 34 * day;
Date date_3_hm_date = new Date(date_3_hm);
return date_3_hm_date;
}
/**
* 得到现在时间
* @return 字符串 yyyyMMdd HHmmss
*/
public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 得到现在小时
*/
public static String getHour() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String hour;
hour = dateString.substring(11, 13);
return hour;
}
/**
* 得到现在分钟
* @return
*/
public static String getTime() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String min;
min = dateString.substring(14, 16);
return min;
}
/**
* 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
* @param sformat
* yyyyMMddhhmmss
* @return
*/
public static String getUserDate(String sformat) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(sformat);
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 时间戳转为时间(年月日,时分秒)
* @param cc_time 时间戳
* @return
*/
public static String getStrTime(String cc_time) {
String re_StrTime = null;
//同理也可以转为其它样式的时间格式.例如:"yyyy/MM/dd HH:mm"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 例如:cc_time=1291778220
long lcc_time = Long.valueOf(cc_time);
re_StrTime = sdf.format(new Date(lcc_time * 1000L));
return re_StrTime;
}
/*将字符串转为时间戳*/
public static long getStringToDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try{
date = sdf.parse(time);
} catch(ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date.getTime();
}
/**
* 掉此方法输入所要转换的时间输入例如("2014年06月14日16时09分00秒")返回时间戳
* @param time ("2014-06-14 16:09:00")
* @return
*/
public static String data(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.CHINA);
Date date;
String times = null;
try {
date = sdr.parse(time);
long l = date.getTime();
String stf = String.valueOf(l);
times = stf.substring(0, 10);
} catch (Exception e) {
e.printStackTrace();
}
return times;
}
/**
* 调用此方法输入所要转换的时间戳输入例如(1402733340)输出("2014-06-14 16:09:00")
* @param time
* @return
*/
public static String timedate(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;
}
/**
* 获取当前年
* @return
*/
public static String getYear(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String s = sdf.format(new Date());
return s;
}
/**
* 获取当前月
* @return
*/
public static String getMonth(){
SimpleDateFormat sdf = new SimpleDateFormat("MM");
String s = sdf.format(new Date());
return s;
}
/**
* 获取当前日
* @return
*/
public static String getDays(){
SimpleDateFormat sdf = new SimpleDateFormat("dd");
String s = sdf.format(new Date());
return s;
}
//将时间戳转化成HH:mm格式 家的可以用
public static String getTime(Long orderStarTime) {
SimpleDateFormat startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = startTime.format(orderStarTime);
return format;
}
}