java 格式化时间计算
生活随笔
收集整理的這篇文章主要介紹了
java 格式化时间计算
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.roshi.utils.common;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtilsCom {/*** 格式化時(shí)間?* @param date* @return* @author lzq* @throws ParseException?* @date 2016年10月14日*/public static String timeFormat(Date date) throws ParseException{SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");String str="";long from = date.getTime(); ?String d=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());long to = simpleFormat.parse(d).getTime(); ?int day = (int) ((to - from)/(1000 * 60 * 60 * 24)); ?// long hour=(l/(60*60*1000)-day*24);day = Math.abs(day);if(day==0){str="今天";}if(day>0&&day<=6){str=day+"天";}if(day>6&&day<30){str=day/7+"周";}if(day>=30){long a = day/30;str=a+"個(gè)月";if(a>=12){str=a/12+"年";}}return str;}/*** 格式化時(shí)間?* @param date* @return* @author lzq* @throws ParseException?* @date 2016年10月14日*/public static String timeFormat(String time) throws ParseException{SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");String str="";long from = simpleFormat.parse(time).getTime(); ?String d=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());long to = simpleFormat.parse(d).getTime(); ?int day = (int) ((to - from)/(1000 * 60 * 60 * 24)); ?// long hour=(l/(60*60*1000)-day*24);day = Math.abs(day);if(day==0){str="今天";}if(day>0&&day<=6){str=day+"天";}if(day>6&&day<30){str=day/7+"周";}if(day>=30){long a = day/30;str=a+"個(gè)月";if(a>=12){str=a/12+"年";}}return str;}public static void main(String [] args) throws ParseException{String d1 = "2016-10-14";String d2 = "2016-10-15";/* 先轉(zhuǎn)成毫秒并求差 */SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String d=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());long m = sdf.parse(d).getTime()- sdf.parse(d2).getTime();/* 根據(jù)你的需求進(jìn)行單位轉(zhuǎn)換 */System.out.println("相差毫秒數(shù):"+ m );System.out.println("相差天數(shù):"+ ( m / (1000 * 60 * 60 * 24) ) );String strDate = "2016-10-25";Date date=sdf.parse(strDate);System.out.println(timeFormat("2016-10-13"));}}
轉(zhuǎn)載于:https://www.cnblogs.com/lizuoqi/p/5977771.html
總結(jié)
以上是生活随笔為你收集整理的java 格式化时间计算的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 防止sql注入的方法
- 下一篇: mac 下开发golang 配置