在开发项目的时候,一般都会有时间转换,然后这边整理了一些时间转换的方法,方便快速去设置需要的格式时间
这个是后来项目中添加的工具类,有一些还没有进行添加上去。
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by Administrator on 2017/9/4.
* description: 时间工具类
*/
public class DateUtils {
public static String DateStyleYMD_HMS = "yyyy-MM-dd HH:mm:ss";
public static String DateStyleYMD_HZZ = "yyyy-MM-dd HH:00:00";
public static String DateStyleYMD_HMZ = "yyyy-MM-dd HH:mm:00";
public static String DateStyleYMD = "yyyy-MM-dd";
public static String DateStyleYMDHM = "yyyy-MM-dd HH:mm";
public static String DateStyleMDHM = "MM-dd HH:mm";
public static String DateStyleHMS = "HH:mm:ss";
public static String DateStyleHM = "HH:mm";
public static String DateStyle_Y = "yyyy";
public static String DateStyle_M = "MM";
public static String DateStyle_D = "dd";
public static String DateStyle_H = "HH";
public static String DateStyle_m = "mm";
public static String DateStyle_S = "SS";
/**
* 根據String和傳入格式返回一个时间戳
*
* @return
*/
public static long getLongDate(String time, String style) {
SimpleDateFormat sdf = new SimpleDateFormat(style);
Date dt = new Date();
long rTime = 0;
try {
dt = sdf.parse(time);//先有string转化为date,
rTime = dt.getTime();//再有date转化为时间戳
} catch (ParseException e) {
e.printStackTrace();
}
return rTime;
}
/**
* 将时间错转换成指定格式的字符串
*
* @param log_startTime
* @param dateStyleHMS
* @return
*/
public static String DateForMumber(long log_startTime, String dateStyleHMS) {
SimpleDateFormat sdf = new SimpleDateFormat(dateStyleHMS);
String re_StrTime = sdf.format(new Date(log_startTime));
return re_StrTime;
}
/**
* 获取当前的时间
*
* @param type
* @return
*/
public static long getNowTime(String type) {
Date date = new Date();
DateFormat format = new SimpleDateFormat(type);
String time = format.format(date);
return getLongDate(time, type);
}
/***
* 根据传过来的String,和type , 转换成要输出的Type
*
* @param time 時間
* @param intoType 傳入格式
* @param inPutType 輸出格式
* @return
*/
public static String upDateState(String time, String intoType, String inPutType) {
return DateForMumber(getLongDate(time, intoType), inPutType);
}
/**
* 年份
*
* @return
*/
public static int year() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.YEAR);
}
/**
* 月份
*
* @return
*/
public static int month() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.MONTH) + 1;
}
/**
* 日(号)
*
* @return
*/
public static int day() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.DAY_OF_MONTH);
}
/**
* 小时(点)
*
* @return
*/
public static int hour() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.HOUR_OF_DAY);
}
/**
* 分钟
*
* @return
*/
public static int minute() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.MINUTE);
}
/**
* 秒
*
* @return
*/
public static int second() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.SECOND);
}
/**
* 获取多少天后的时间搓
*
* @param day
* @return
*/
public static long getHowDays(int day) {
//获取当前时间
long nowTime = getNowTime(DateStyleYMD);
//获取7天后的时间时间搓
long addTime = 24 * 60 * 60 * day * 1000;
return nowTime + addTime;
}
/**
* 获取多少天后的时间搓
*
* @param startDay 输入时间
* @param type 输入时间格式
* @param day 多少天之后
* @return
*/
public static long getHowDays(String startDay, String type, int day) {
long startTime = getLongDate(startDay, type);
//获取7天后的时间时间搓
long addTime = 24 * 60 * 60 * day * 1000;
return startTime + addTime;
}
}