java當前日期,java當前日期加一個月

本文目錄一覽:

java如何獲取當前時間 年月日 時分秒

java如何獲取當前時間以及格式化需要用到兩個類,如下圖:

1.獲取當前時間,並格式化為(年-月-日 時:分:秒)。

Date t = new Date();

SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

System.out.println(df.format(t));

列印輸出結果如下圖:

2.將java.util.Date轉換為java.sql.Date格式。

java.sql.Date sqld = new java.sql.Date(t.getTime());

System.out.println(sqld);

java.sql.Time sqlt = new java.sql.Time(t.getTime());

System.out.println(sqlt);

java.sql.Timestamp sqlts = new java.sql.Timestamp(t.getTime());

System.out.println(sqlts);

列印輸出結果如下圖:

「拓展資料——java」:

Java是一種廣泛使用的計算機編程語言,擁有跨平台、面向對象、泛型編程的特性,廣泛應用於企業級Web應用開發和移動應用開發。

Java編程語言的風格十分接近C++語言。繼承了C++語言面向對象技術的核心,捨棄了容易引起錯誤的指針,以引用取代;移除了C++中的運算符重載和多重繼承特性,用介面取代;增加垃圾回收器功能。

Java編程語言是個簡單、面向對象、分散式、解釋性、健壯、安全與系統無關、可移植、高性能、多線程和動態的語言。

java 獲取當前日期,應該如何操作呢

package util;

import java.math.BigDecimal;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/**

* 獲取系統時間

*

*/

public class DateUtil {

/* 日誌對象 */

// private static Logger logger = Logger.getLogger(SystemUtil.class);

/* 獲取年份 */

public static final int YEAR = 1;

/* 獲取年月 */

public static final int YEARMONTH = 2;

/* 獲取年月日 */

public static final int YEARMONTHDAY = 3;

/* 獲取年月日,小時 */

public static final int YMD_HOUR = 4;

/* 獲取年月日,小時,分鐘 */

public static final int YMD_HOURMINUTE = 5;

/* 獲取年月日,時分秒 */

public static final int FULL = 6;

/* 獲取年月日時分秒 格式:yyyyMMddHHmmss */

public static final int UTILTIME = 7;

/**

* 根據指定時間格式類型得到當前時間

*

* @param type

* 時間類型

* @return String 字元串時間

*/

public static synchronized String getCurrentTime(int type) {

String format = getFormat(type);

SimpleDateFormat timeformat = new SimpleDateFormat(format);

Date date = new Date();

return timeformat.format(date);

}

/**

* 返回當前系統時間的年月日

*

* @return

*/

public static synchronized String getCurrentTime() {

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

Date date = new Date();

return timeformat.format(date);

}

/**

* 根據參數格式,格式化當前日期

* @param format

* @return

*/

public static synchronized String getDateString(String format) {

SimpleDateFormat timeformat = new SimpleDateFormat(format);

Date date = new Date();

return timeformat.format(date);

}

/**

* 根據指定時間格式類型,格式化時間格式

*

* @param type

* 時間格式類型

* @return

*/

private static String getFormat(int type) {

String format = “”;

if (type == 1) {

format = “yyyy”;

} else if (type == 2) {

format = “yyyy-MM”;

} else if (type == 3) {

format = “yyyy-MM-dd”;

} else if (type == 4) {

format = “yyyy-MM-dd HH”;

} else if (type == 5) {

format = “yyyy-MM-dd HH:mm”;

} else if (type == 6) {

format = “yyyy-MM-dd HH:mm:ss”;

} else if (type == 7) {

format = “yyyyMMddHHmmss”;

} else {

throw new RuntimeException(“日期格式參數錯誤”);

}

return format;

}

public static int getYear(String dateString) {

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

Date date = null;

try {

date = dd.parse(dateString);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

return cal.get(Calendar.YEAR);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public static int getMonth(String dateString) {

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

Date date = null;

try {

date = dd.parse(dateString);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

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

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public static int getDay(String dateString) {

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

Date date = null;

try {

date = dd.parse(dateString);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

return cal.get(Calendar.DAY_OF_MONTH);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public static Date StringToDate(String dateStr, String formatStr) {

SimpleDateFormat dd = new SimpleDateFormat(formatStr);

Date date = null;

try {

date = dd.parse(dateStr);

} catch (ParseException e) {

e.printStackTrace();

}

return date;

}

/**

* 當前日期和參數日期距離的小時數 日期格式:yyyy-MM-dd HH:mm:ss

*

* @param date

* @return

*/

public static double getHours(String date) {

SimpleDateFormat timeformat = new SimpleDateFormat(

“yyyy-MM-dd HH:mm:ss”);

try {

Date d = new Date();

Date d1 = timeformat.parse(date);

long temp = d.getTime() – d1.getTime();

double f = temp / 3600000d;

BigDecimal b = new BigDecimal(f);

double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

return f1;

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

public static void main(String a[]) {

try {

int aa = getYear(“2012-01-08”);

System.out.println(aa);

} catch (Exception e) {

e.printStackTrace();

}

}

}

JAVA中獲取系統當前時間該怎麼寫?

一. 獲取當前系統時間和日期並格式化輸出:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0apublic class NowString { \x0d\x0a public static void main(String[] args) { \x0d\x0a SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);//設置日期格式\x0d\x0a System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間\x0d\x0a } \x0d\x0a} \x0d\x0a\x0d\x0a二. 在資料庫里的日期只以年-月-日的方式輸出,可以用下面兩種方法:\x0d\x0a\x0d\x0a1、用convert()轉化函數:\x0d\x0a\x0d\x0aString sqlst = “select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;\x0d\x0a\x0d\x0aSystem.out.println(rs.getString(“convertBookDate”)); \x0d\x0a\x0d\x0a2、利用SimpleDateFormat類:\x0d\x0a\x0d\x0a先要輸入兩個java包:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0a然後:\x0d\x0a\x0d\x0a定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);\x0d\x0a\x0d\x0asql語句為:String sqlStr = “select bookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;\x0d\x0a\x0d\x0a輸出:\x0d\x0a\x0d\x0aSystem.out.println(df.format(rs.getDate(“bookDate”)));

java怎麼獲取當前日期

java中的Date類,實例化即可獲取當前日期:

(import java.util.Date;

import java.text.SimpleDateFormat; )

代碼:

Date now = new Date();

SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd”);

String curDate= dateFormat.format( now );

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-14 02:16
下一篇 2024-12-14 02:16

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

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

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

    編程 2025-04-29
  • Java JsonPath 效率優化指南

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

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

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

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

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

    編程 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
  • VSCode為什麼無法運行Java

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

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

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

    編程 2025-04-29

發表回復

登錄後才能評論