導航:首頁 > 編程語言 > javadateint

javadateint

發布時間:2023-01-01 14:48:04

java語言中的date類及方法的用法

Date和Calendar是Java類庫里提供對時間進行處理的類,由於日期在商業邏輯的應用中占據著很重要的地位,所以在這里想對這兩個類進行一個基本的講解,由於技術有限,不到之處請指正。

Date類顧名思義,一看就知道是和日期有關的類了,這個類最主要的作用就是獲得當前時間了,然而這個類裡面也具有設置時間以及一些其他的功能,可是由於本身設計的問題,這些方法卻遭到眾多批評,而這些遭受批評的功能都已移植到另外一個類裡面,這就是今天要講到的第二個類Calendar裡面。

在講兩個類之前,這里又不能不多提一個類,那就是DateFormat類,這個類是用來格式化日期的,稍後也會講到。

首先,讓我們來看一個獲取當前時間的例子:

Date date = new Date();
System.out.println(date.getTime());上面的語句首先創建了Date的一個對象,接著使用getTime方法獲得當前的時間,但是注意了,輸出後的結果確實一串長整型的數字,這是為什麼?實際上這是系統根據當前時間計算出來的一個long型的數,至於是如何計算出來的就不在本文中講述了,那既然這樣的話又如何顯示正確的時間呢?這就要利用到上面的DateFormat類了,這個類是一個基類,它有一個子類是SimpleDateFormat,具體用法請看下面的代碼:

Date date = new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
System.out.println(dateFm.format(date));這段代碼開始創建了一個Date的對象,用來獲取當前時間,而重點就在於後面的SimpleDateFormat對象,這個對繼承了DateFormat,利用format方法對Date對象進行格式化,然後輸出,而格式的定製是由用戶定製的,EEEE代表星期,MMMM代表月份,而dd代表日,yyyy代表年。使用這個方法就可以根據用戶自定義的格式進行輸出時間。

上面介紹了由用戶自定義格式的輸出時間,下面將來介紹通過JAVA類庫提供的標准格式輸出時間,這就要用到DateFormat類了,請看以下代碼:

Date date = new Date();
DateFormat dateFm = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT);
System.out.println(dateFm.format(date));這里使用的方法和用戶自定義的方法差不多,只是這里使用的是一個抽象類,由於DateFormat是一個抽象類,所以它不能通過構造函數構造對象,在這里是通過getDateTimeInstance()方法獲得該對象,而所傳遞的參數就是DateFormat裡面定義的一些常量,系統根據這些常量輸出當前時間,由於這里使用的是getDateTimeInstance方法,所以將傳遞兩個常量參數,用來分別格式化日期和當前的時間。

上面講述了如何獲得系統時間以及如何格式化輸出,那如果想獲取或者設置時間當中的某一部分又該如何呢?例如年,月,日。這就要靠Calendar這個類了,這個類也是一個抽象類,它有一個子類GregorianCalendar,接下來我會利用這個子類來演示這個過程,請看以下代碼:

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(new Date());

System.out.println("System Date: " + dateFormat.format(cal.getTime()));

cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.FRIDAY);
System.out.println("After Setting Day of Week to Friday: " +
dateFormat.format(cal.getTime()));
這段代碼當中,首先創建了一個DateFormat對象進行格式設置,接著創建了一個GregorianCalendar對象cal,接著使用cal.setTime()方法設置cal對象中的時間為當前時間,然後通過format格式化由cal.getTime()返回的時間進行輸出,後面利用set方法設置cal的日期為當前星期的FRIDAY,此時cal中存儲的時間就是這個星期五的該時刻,而後面利用format格式化輸出,假如當前時間為2005年1月27日星期4的11點30分,那麼最後將那句將會輸出2005年1月28日星期5的11點30分。

㈡ java中int轉換成date

Date類與其它數據類型的相互轉換整型和Date類之間並不存在直接的對應關系,只是你可以使用int型為分別表示年、月、日、時、分、秒,這樣就在兩者之間建立了一個對應關系,在作這種轉換時,你可以使用Date類構造函數的三種形式:Date(int year, int month, int date):以int型表示年、月、日Date(int year, int month, int date, int hrs, int min):以int型表示年、月、日、時、分Date(int year, int month, int date, int hrs, int min, int sec):以int型表示年、月、日、時、分、秒\r在長整型和Date類之間有一個很有趣的對應關系,就是將一個時間表示為距離格林尼治標准時間1970年1月1日0時0分0秒的毫秒數。對於這種對應關系,Date類也有其相應的構造函數:Date(long date)獲取Date類中的年、月、日、時、分、秒以及星期你可以使用Date類的getYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()、getDay()方法,你也可以將其理解為將Date類轉換成int。

㈢ java.util.Date類型怎麼插入資料庫欄位是int型的

Date date = new Date(); //模擬Date類型的一個對象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); //規范日期格式為:年月日

int i = Integer.parseInt(dateFormat.format(date)); //完成轉換

㈣ java.sql.Date(int,int,int)中的三個參數分別指的是什麼

Date(int year, int month, int day)
已過時。 使用構造方法 Date(long date) 替代

㈤ Java 程序中:Date date = sdf.parse(hisParams.getBgtime()); 這個date怎麼轉換為int型

date.getTime(); 是long類型。1970年以來的毫秒數,如果要轉為int要除以1000,轉為秒。

㈥ java中的Date類為什麼很多方法被廢棄了

Date類中有很多方法都標有刪除線,是因為Date類在設計中有很多問題,如getYear指的是1900年以來的年數,getMonth是從0開始的。事實上,不止Date類,Java的其實時間相關類都存在設計問題,以下舉些例子,並提供解決方案。

我們通常使用 Date和Calander用作時間處理,其實會有兩個問題:
1.Date的缺陷,我們知道 Date的setYear和getYear等函數是刪除線顯示的
原因在:比如今天是2009-01-04日,那麼獲取的年竟然是109,所以是有問題的

2.Calender常常用於時間的回卷,經常使用的就是roll(Day_of_Year,-7)就是七天前
但是如果是2009-01-04日,那麼七天前是2009-12-28日,而非2008年,這是因為它只對天回卷了,年沒有回卷

3、針對這些問題,提供一套日期工具類:
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));
}
}

㈦ 資料庫中 number date 類型 在java的封裝類中 應該用什麼類型int 還是string

你連資料庫查詢返回會得到一個ResultSet對象的實例,可以通過getInt(),getDate()得到數值和日期。注意的是,即使是NUMBER型,也推薦用getString來獲得,畢竟字元串好操作,這樣返回util.String型;至於date,返回的是java.sql.Date

㈧ java 獲取當天日期,並將日期轉化成數值。

import java.text.SimpleDateFormat;import java.util.Date;
public class Timedemo {
public static void main(String[] args)
{
long time=System.currentTimeMillis();
Date date=new Date(time);
String mat="yyyy-MM-dd";
String ma="yyyyMMdd";
SimpleDateFormat format=new SimpleDateFormat(mat);
SimpleDateFormat forma=new SimpleDateFormat(ma);
String nowdate=format.format(date);
String nwdate=forma.format(date);
int x=Integer.parseInt(nwdate);
System.out.println(nowdate);
System.out.println(nwdate);
System.out.println(x);
}
}

經過測試滿足以上條件 希望對你有幫助

閱讀全文

與javadateint相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:142
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:732
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:141
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:890
app轉賬是什麼 瀏覽:163