新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
日期操作工具類
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * 時間工具類
- * Created by zyb on 2016年3月15日.
- */
- public class DateTimeUtil {
- /**
- * 獲取當(dāng)前系統(tǒng)的日期和時間,返回格式為:2015-10-23 14:00:46
- *
- * @return String
- */
- public static String getDateTime() {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str = df.format(new Date());
- return str;
- }
- /**
- * 獲取當(dāng)前系統(tǒng)的日期和時間,返回格式為:20151023140046
- *
- * @return
- */
- public static String getDateTimeExt() {
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
- String str = df.format(new Date());
- return str;
- }
- /**
- * 獲取當(dāng)前系統(tǒng)的日期,返回格式為:2015-10-23
- *
- * @return String
- */
- public static String getDate() {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- String str = df.format(new Date());
- return str;
- }
- /**
- * 獲取當(dāng)前系統(tǒng)的日期,返回格式為:2015/10/23
- *
- * @return
- */
- public static String getDateExt() {
- SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
- String str = df.format(new Date());
- return str;
- }
- /**
- * 當(dāng)前周
- *
- * @return
- */
- public static String getWeek() {
- Calendar cd = Calendar.getInstance();
- String strWeek = String.valueOf(cd.get(Calendar.WEEK_OF_YEAR));
- return strWeek;
- }
- /**
- * 當(dāng)前年
- *
- * @return
- */
- public static String getYear() {
- Calendar cd = Calendar.getInstance();
- String strYear = String.valueOf(cd.get(Calendar.YEAR));
- return strYear;
- }
- /**
- * 當(dāng)前月
- *
- * @return
- */
- public static String getMonth() {
- Calendar cd = Calendar.getInstance();
- String strMonth = String.valueOf(cd.get(Calendar.MONTH) + 1);
- return strMonth;
- }
- /**
- * 獲取日期
- *
- * @return
- */
- public static String getDay() {
- Calendar cd = Calendar.getInstance();
- String strDay = String.valueOf(cd.get(Calendar.DAY_OF_MONTH));
- return strDay;
- }
- /**
- * 獲取當(dāng)前時間差
- *
- * @return String
- */
- public static long deltaT(String strDateTime) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date now = null, date = null;
- try {
- now = df.parse(getDateTime());
- date = df.parse(strDateTime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- long l = now.getTime() - date.getTime();
- long day = l / (24 * 60 * 60 * 1000);
- long hour = (l / (60 * 60 * 1000) - day * 24);
- long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
- long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
- System.out.println("" + day + "天" + hour + "小時" + min + "分" + s + "秒");
- return day;
- }
- public static void main(String[] args) throws Exception {
- System.out.println(deltaT("2016-03-11 18:02:50"));
- }
- }
【本文是專欄作者張勇波的原創(chuàng)文章,轉(zhuǎn)載請通過獲取作者授權(quán)】

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比泉山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式泉山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋泉山地區(qū)。費用合理售后完善,10年實體公司更值得信賴。
新聞名稱:日期操作工具類
網(wǎng)頁鏈接:http://m.fisionsoft.com.cn/article/dhgdjhd.html


咨詢
建站咨詢
