您的位置:首页 > 其它

时间上的格式化

2016-01-22 00:00 309 查看
//获取当前时间long类型
public static long getTimeInSecond() {
long time = System.currentTimeMillis();

return (time / 1000L);
}

public static String getTimeStringInMinute() {
Calendar calendar = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("HH:mm");

return dateFormat.format(calendar.getTime());

}
//long类型时间转换为需要的类型
public static String getTimeLongInDate(Long time,String format){
SimpleDateFormat sdf= new SimpleDateFormat(format);
//前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型
Date dt = new Date(time * 1000);
String sDateTime = sdf.format(dt);
return sDateTime;
}
//时间格式化
public static String getTimeLongDate(Long time){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
//前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型
Date dt = new Date(time * 1000);
String sDateTime = sdf.format(dt);
return sDateTime;
}

//获取3个月前的时间
public static String getBeforeTime(){
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(calendar.MONTH, -3); //设置为前3月
dBefore = calendar.getTime(); //得到前3月的时间
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
return defaultStartDate;
}

/**
* 获取当前时间
* @return
*/
public static String getCurrentDate(String dateFormat) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
String dateString = formatter.format(currentTime);
return dateString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: