java獲取當前日期,Java獲取當前日期是第幾周

本文目錄一覽:

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

一. 獲取當前系統時間和日期並格式化輸出:

import java.util.Date;

import java.text.SimpleDateFormat;

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);//設置日期格式

System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間

}

}

二. 在數據庫里的日期只以年-月-日的方式輸出,可以用下面兩種方法:

1、用convert()轉化函數:

String sqlst = “select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;

System.out.println(rs.getString(“convertBookDate”));

2、利用SimpleDateFormat類:

先要輸入兩個java包:

import java.util.Date;

import java.text.SimpleDateFormat;

然後:

定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql語句為:String sqlStr = “select bookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;

輸出:

System.out.println(df.format(rs.getDate(“bookDate”)));

java如何獲取當前日期並計算出2個月後的日期

//0、日期輸出格式

SimpleDateFormat f = new SimpleDateFormat(“yyyy/MM/dd,HH:mm:ss”);

//1、獲取當前日期 方式一

Date b = new Date();

System.out.println(f.format(b));

//2、獲取當前日期 方式二

Calendar c = Calendar.getInstance();

//可以手動設置日期

//c.set(2011, Calendar.JANUARY, 31);

System.out.println(f.format(c.getTime()));

//3、獲取當前日期增加兩個月後的日期,

c.add(Calendar.MONTH, 2);

System.out.println(f.format(c.getTime()));

如何用java語言 獲得系統當前日期

java語言 獲得系統當前日期:

1、Date date=new Date();這個是java提供的時間類,可以從中取出,年、月日、時、分、秒

2、SimpleDateFormat這個是時間格式類,對時間進行格式化

String time=new SimpleDateFormat(“HH:mm:ss”).format(new Date())

time=15:02:03

String time=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(new Date())

time=2015-05-26 15:02:03

3、System.currentTimeMillis(),返回的是long型日期時間

long time=System.currentTimeMillis();

time=352632563256;

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中如何獲取當前時間。

方法一:用java.util.Date類來實現,並結合java.text.DateFormat類來實現時間的格式化,看下面代碼:

import java.util.*;

import java.text.*;

//以下默認時間日期顯示方式都是漢語語言方式

//一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53

//以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現見類TestDate2.java

public class TestDate {

public static void main(String[] args) {

Date now = new Date();

Calendar cal = Calendar.getInstance();

DateFormat d1 = DateFormat.getDateInstance(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如:2008-6-16 20:54:53)

String str1 = d1.format(now);

DateFormat d2 = DateFormat.getDateTimeInstance();

String str2 = d2.format(now);

DateFormat d3 = DateFormat.getTimeInstance();

String str3 = d3.format(now);

DateFormat d4 = DateFormat.getInstance(); //使用SHORT風格顯示日期和時間

String str4 = d4.format(now);

DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時間(精確到秒)

String str5 = d5.format(now);

DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時間(精確到秒)

String str6 = d6.format(now);

DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時間(精確到分)

String str7 = d7.format(now);

DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //顯示日期,時間(精確到分)

String str8 = d8.format(now);//與SHORT風格相比,這種方式最好用

System.out.println(“用Date方式顯示時間: ” + now);//此方法顯示的結果和Calendar.getInstance().getTime()一樣

System.out.println(“用DateFormat.getDateInstance()格式化時間後為:” + str1);

System.out.println(“用DateFormat.getDateTimeInstance()格式化時間後為:” + str2);

System.out.println(“用DateFormat.getTimeInstance()格式化時間後為:” + str3);

System.out.println(“用DateFormat.getInstance()格式化時間後為:” + str4);

System.out.println(“用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間後為:” + str5);

System.out.println(“用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間後為:” + str6);

System.out.println(“用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間後為:” + str7);

System.out.println(“用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間後為:” + str8);

}

}

運行結果:

用Date方式顯示時間: Mon Jun 16 20:54:53 CST 2008

用DateFormat.getDateInstance()格式化時間後為:2008-6-16

用DateFormat.getDateTimeInstance()格式化時間後為:2008-6-16 20:54:53

用DateFormat.getTimeInstance()格式化時間後為:20:54:53

用DateFormat.getInstance()格式化時間後為:08-6-16 下午8:54

用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間後為

:2008年6月16日 星期一 下午08時54分53秒 CST

用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間後為

:2008年6月16日 下午08時54分53秒

用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間後

為:08-6-16 下午8:54

用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間

後為:2008-6-16 20:54:53

方法二:用java.util.Calendar類來實現,看下面:

import java.util.*;

import java.text.*;

//以下是利用Calendar類來實現日期時間的,和Date類相比較比較簡單

public class TestDate2 {

public static void main(String[] args) {

Calendar ca = Calendar.getInstance();

int year = ca.get(Calendar.YEAR);//獲取年份

int month=ca.get(Calendar.MONTH);//獲取月份

int day=ca.get(Calendar.DATE);//獲取日

int minute=ca.get(Calendar.MINUTE);//分

int hour=ca.get(Calendar.HOUR);//小時

int second=ca.get(Calendar.SECOND);//秒

int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);

System.out.println(“用Calendar.getInstance().getTime()方式顯示時間: ” + ca.getTime());

System.out.println(“用Calendar獲得日期是:” + year +”年”+ month +”月”+ day + “日”);

System.out.println(“用Calendar獲得時間是:” + hour +”時”+ minute +”分”+ second +”秒”);

System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那麼顯示6)

}

}

運行結果是:

用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008

用Calendar獲得日期是:2008年5月16日

用Calendar獲得時間是:9時54分21秒

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
HDAO的頭像HDAO
上一篇 2024-10-10 09:25
下一篇 2024-10-10 09:25

相關推薦

  • Java JsonPath 效率優化指南

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

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

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

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

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

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

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

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

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

    編程 2025-04-29

發表回復

登錄後才能評論