java這周到現在多少秒,java當前毫秒數

本文目錄一覽:

java整么計算時間

提供一個時間操作類給你,你可以調用裡面的dateDiff方法,dateDiff(new Date(), getDateFromString(“明天的字元串”)表示的就是現在到明天0點之前的差距天數。

你要分鐘數的話就自己改一下dateDiff,不要除以60和24就是分鐘了

以下是工具類代碼

import org.apache.log4j.Logger;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class AdDateUtil {

    private static Logger logger = Logger.getLogger(AdDateUtil.class);

    static public String getNowStr(String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        String now = sdf.format(new Date());

        return now;

    }

    static public Date getFormatDate(String date, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        Date d = new Date();

        try {

            d = sdf.parse(date);

        } catch (ParseException e) {

            logger.error(e);

        }

        return d;

    }

    static public String getDateStr(Date date, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        String d = sdf.format(date);

        return d;

    }

    static public String getPadZeroString(String s, int size) {

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i  (size – s.length()); i++) {

            sb.append(“0”);

        }

        sb.append(s);

        return sb.toString();

    }

    /**

    * 得到某月的天數

    *

    * @param year

    * @param month

    * @return

    */

    static public int getDayCountOfMonth(String year, String month) {

        Calendar cal = Calendar.getInstance();

        // 年

        cal.set(Calendar.YEAR, Integer.parseInt(year));

        // 月,因為Calendar里的月是從0開始,所以要-1

        cal.set(Calendar.MONTH, Integer.parseInt(month) – 1);

        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    }

    static public String getYesterday(String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar now = Calendar.getInstance();

        now.roll(Calendar.DAY_OF_YEAR, -1); //昨天

        return df.format(now.getTime());

    }

    /**

    * 獲取和今天附近的某天

    * @param format

    * @param diff

    * @return

    */

    static public String getADay(String format, int diff) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar now = Calendar.getInstance();

        int beforeM = now.get(Calendar.MONTH);

        now.roll(Calendar.DAY_OF_YEAR, diff); //

        int nowM = now.get(Calendar.MONTH);

        //必須進行日期處理,否則2009-01-04日前七天是2009-12-28

        if (nowM  beforeM) {

            now.roll(Calendar.YEAR, -1);

        }

        return df.format(now.getTime());

    }

    static public String getTomorrow(String format) {

        SimpleDateFormat df = new SimpleDateFormat(format);

        Calendar now = Calendar.getInstance();

        now.roll(Calendar.DAY_OF_YEAR, 1); //明天

        return df.format(now.getTime());

    }

    /**

    * 得到最近num天的全部日期

    * 說明:

    * 1.日期是從昨天開始算的.

    * 2.如果num=2 , 日期是2008-03-14 ,則返回的結果為 2008-03-12、2008-03-13

    * @param num

    * @return

    */

    public static String[] getDaysByNum(int num, String date) {

        SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

        String[] result = {  };

        Calendar cal = Calendar.getInstance();

        cal.setTime(getDateFromString(date, “yyyy-MM-dd”));

        //最近一周

        result = new String[num];

        for (int i = num; i  0; i–) {

            cal.add(Calendar.DAY_OF_YEAR, -1);

            result[i – 1] = sdf.format(new Date(cal.getTimeInMillis()));

        }

        return result;

    }

    public static Date getDateFromString(String dateStr, String format) {

        if ((dateStr == null) || (format == null)) {

            try {

                throw new Exception(“數據類型異常” + dateStr + “|” + format);

            } catch (Exception e) {

                logger.error(“數據類型異常:” + e);

            }

        }

        SimpleDateFormat df = new SimpleDateFormat(format);

        Date date;

        try {

            date = df.parse(dateStr);

            return date;

        } catch (Exception ex) {

            logger.error(ex);

            return new Date();

        }

    }

    static public int getNowYear() {

        Calendar cal = Calendar.getInstance();

        return cal.get(Calendar.YEAR);

    }

    static public int getNowMonth() {

        Calendar cal = Calendar.getInstance();

        return cal.get(Calendar.MONTH) + 1;

    }

    public static String[] getMonthRang(String year, String month) {

        String beginDate = year + “-” + month + “-01”;

        String endDate = year + “-” + month + “-” +

            getDayCountOfMonth(year, month);

        return getDaysByRang(beginDate, endDate);

    }

    public static String[] getDaysByRang(String beginDate, String endDate) {

        SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

        //得到兩個日期間相差多少天

        int num = dateDiff(beginDate, endDate);

        if (num  0) {

            //顛倒一下日期

            String tmp = beginDate;

            beginDate = endDate;

            endDate = tmp;

            num = 0 – num;

        }

        String[] result = {  };

        Calendar cal = Calendar.getInstance();

        try {

            cal.setTime(sdf.parse(beginDate));

        } catch (ParseException e) {

            e.printStackTrace();

        }

        num = num + 1; //把開始和結束日期都包含進去

        result = new String[num];

        for (int i = 0; i  num; i++) {

            if (i  0) {

                cal.add(Calendar.DAY_OF_YEAR, 1);

            }

            result[i] = sdf.format(new Date(cal.getTimeInMillis()));

        }

        return result;

    }

    public static int dateDiff(String beginDate, String endDate) {

        SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

        Date date = null;

        try {

            date = sdf.parse(endDate);

        } catch (ParseException e) {

            date = new Date();

            e.printStackTrace();

        }

        long end = date.getTime();

        try {

            date = sdf.parse(beginDate);

        } catch (ParseException e) {

            date = new Date();

            e.printStackTrace();

        }

        long begin = date.getTime();

        long day = (end – begin) / (1000 * 3600 * 24); //除1000是把毫秒變成秒

        return Integer.parseInt(Long.toString(day));

    }

    public static void main(String[] args) {

        System.out.println(AdDateUtil.getADay(“yyyy-MM-dd”, -7));

    }

}

java 獲取當前日期時間和本周的星期一的日期時間?

提倡了一下中文寫,…

static public void main(String 參數[]){

SimpleDateFormat 格式=new SimpleDateFormat(“y年M月d日 E H時m分s秒”,Locale.CHINA);

Calendar 日曆=Calendar.getInstance(Locale.CHINA);

//當前時間,貌似多餘,其實是為了所有可能的系統一致

日曆.setTimeInMillis(System.currentTimeMillis());

System.out.println(“當前時間:”+格式.format(日曆.getTime()));

日曆.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

System.out.println(“周一時間:”+格式.format(日曆.getTime()));

}

===========

改到後天測了一下

輸出

當前時間:2011年8月31日 星期三 12時32分40秒

周一時間:2011年8月29日 星期一 12時32分40秒

java計算一個月有多少秒

有2592000(秒)。

jαVα是一個應用程序,每個月都不一樣,但可以選定一個月為30天,就是30(天)*24(小時)*3600(秒)=2592000(秒)。

原創文章,作者:YURB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/149891.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
YURB的頭像YURB
上一篇 2024-11-05 16:53
下一篇 2024-11-05 16:53

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • 現在鏡像站不行了,怎麼辦?

    現在鏡像站不行了是很常見的問題,本文將從多個方面對此問題進行詳細闡述,為大家提供解決方案。 一、檢查網路環境 鏡像站不通常見的原因之一是網路問題,可能是個人網路不穩定,也可能是IS…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論