android 获取系统时间的时间戳 ,时间戳日期互转,计算日期时间差

 获取系统时间戳

 

public String getTime(){
    long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
    String  str=String.valueOf(time);
    return str;
}

 

 

 

 

、获取系统时间

 
SimpleDateFormat df = new SimpleDateFormat("HH");//设置日期格式"yyyy年-MM月dd日-HH时mm分ss秒"

String dfd = df.format(new Date());


结果如下

 

 

2017年-05月26日-14时49分29秒

 

时间戳转换日期

 

public static String timetodate(String time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(Long.valueOf(time));
    SimpleDateFormat sf = new SimpleDateFormat("MM-dd ");//这里的格式可换"yyyy年-MM月dd日-HH时mm分ss秒"等等格式

    String date = sf.format(calendar.getTime());
    return date;

}

 

 

 

时间日期转换成时间戳

 

/*
   * 将时间转换为时间戳
   */
public static String dateToStamp(String s) throws ParseException {
    String res;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = simpleDateFormat.parse(s);
    long ts = date.getTime();
    res = String.valueOf(ts);
    return res;
}

 

/**
 * 四舍五入到小数点后scale位
 * @param v
 * @param scale
 * @return
 */
public static float round(float v, int scale)
{
    if (scale < 0)
        throw new IllegalArgumentException("The scale must be a positive integer or zero");

    BigDecimal bgNum1 = new BigDecimal(Float.toString(v));
    BigDecimal bgNum2 = new BigDecimal("1");
    return bgNum1.divide(bgNum2, scale, BigDecimal.ROUND_HALF_UP).floatValue();
    // return b.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue();
}

转换日期时间格式

public static String getDistanceTimeToHM(String str1) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String typestring = "yyyy-MM-dd HH:mm";
    String endtime="";
    Date one;
    try {
        one = df.parse(str1);
        endtime = dateToString(one,typestring);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return endtime;
}
public static String dateToString(Date data, String formatType) {
    return new SimpleDateFormat(formatType).format(data);
}

如2017-11-01 09:16:03.0转换后是2017-11-01 09:16

 

计算两个日期时间差,天数,时、分、秒、

 

public  int getDistanceTime(String end, String start) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date one;
    Date two;
    long day = 0;
    long hour = 0;
    long min = 0;
    long sec = 0;
    try {

        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        one = df.parse(end);
        c.setTime(one);
        two = df.parse(start);
        long time1 = one.getTime();
        long time2 = two.getTime();
        long diff ;
        diff = time1 - time2;

        day = diff / (24 * 60 * 60 * 1000);//天数
        Log.i("lgq","tian--==="+day+"...one="+end+"..-----.two==="+start);
        hour = (diff / (60 * 60 * 1000) - day * 24);//时
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);//分
        sec = ((diff/1000)- day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return (int)sec;
}

你可能感兴趣的:(android 获取系统时间的时间戳 ,时间戳日期互转,计算日期时间差)