package com.lh.oa.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by DongBang on 2016/5/11.
*/
public class DateUtils {
// 把日期转为字符串
public static String ConverToString(Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
// 把字符串转为日期
public static Date ConverToDate(String strDate) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.parse(strDate);
}
/****************************************************************/
// 将指定日期转化为秒数
public static int getSecondsFromDate(String expireDate) {
if (expireDate == null || expireDate.trim().equals(""))
return 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(expireDate);
return (int) (date.getTime() / 1000);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
//秒数转化为日期
public static String getDateFromSeconds(String seconds){
if(seconds==null)
return " ";
else{
Date date=new Date();
try{
date.setTime(Long.parseLong(seconds)*1000);
}
catch(NumberFormatException nfe){
nfe.printStackTrace();
return "Input string:"+seconds+"not correct,eg:2011-01-20";
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
/**********************************************************************************/
/**
* 毫秒转换成String 日期 格式 yyyy-MM-dd hh :mm:ss
*
* @param ms
* @return
*/
public static String msToDate(long ms) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
return formatter.format(calendar.getTime());
}
public static String msToSimpleDate(long ms) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
return formatter.format(calendar.getTime());
}
/**
* 获取当前时间 格式:yyyy-MM-dd
*
* @return
*/
public static String getCurrentDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
String str = formatter.format(curDate);
return str;
}
public static String getCurrentTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
String str = formatter.format(curDate);
return str;
}
/**
* 获取当月第一天
*
* @return
*/
public static Date getFirstdayofThisMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
public static String getSubDate(String date) {
String subDate = date.substring(0, 10);
return subDate;
}
}
/*******************************************activity中页面************提3天*****************************************************/
DateUtils.getSecondsFromDate(DateUtils.getCurrentTime());
query_start_time_tv.setText("");
System.out.println("输出的当前时间转换成秒数:"+(DateUtils.getSecondsFromDate(DateUtils.getCurrentTime())-3600*3*24*1000));
System.out.println("毫秒转成日期"+DateUtils.getDateFromSeconds(String.valueOf((DateUtils.getSecondsFromDate(DateUtils.getCurrentTime())-3600*3*24))));