java 时间工具类

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))));
 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值