當前位置:首頁 » 編程語言 » javadate

javadate

發布時間: 2022-01-10 11:50:08

java中Date的使用

java語言中的date類介紹及使用
在JDK1.0中,Date類是唯一的一個代表時間的類,但是由於Date類不便於實現國際化,所以從JDK1.1版本開始,推薦使用Calendar類進行時間和日期處理。這里簡單介紹一下Date類的使用。
1、使用Date類代表當前系統時間
Date d = new Date();
System.out.println(d);
使用Date類的默認構造方法創建出的對象就代表當前時間,由於Date類覆蓋了toString方法,所以可以直接輸出Date類型的對象,顯示的結果如下:
Sun Mar 08 16:35:58 CST 2009
在該格式中,Sun代表Sunday(周日),Mar代表March(三月),08代表8號,CST代表China Standard Time(中國標准時間,也就是北京時間(東八區))。
2、使用Date類代表指定的時間
Date d1 = new Date(2009-1900,3-1,9);
System.out.println(d1);
使用帶參數的構造方法,可以構造指定日期的Date類對象,Date類中年份的參數應該是實際需要代表的年份減去1900,實際需要代表的月份減去1以後的值。例如上面的示例代碼代表就是2009年3月9號。
實際代表具體的年月日時分秒的日期對象,和這個類似。
3、獲得Date對象中的信息
Date d2 = new Date();
//年份
int year = d2.getYear() + 1900;
//月份
int month = d2.getMonth() + 1;
//日期
int date = d2.getDate();
//小時
int hour = d2.getHours();
//分鍾
int minute = d2.getMinutes();
//秒
int second = d2.getSeconds();
//星期幾
int day = d2.getDay();
System.out.println("年份:" + year);
System.out.println("月份:" + month);
System.out.println("日期:" + date);
System.out.println("小時:" + hour);
System.out.println("分鍾:" + minute);
System.out.println("秒:" + second);
System.out.println("星期:" + day);
使用Date類中對應的get方法,可以獲得Date類對象中相關的信息,需要注意的是使用getYear獲得是Date對象中年份減去1900以後的值,所以需要顯示對應的年份則需要在返回值的基礎上加上1900,月份類似。在Date類中還提供了getDay方法,用於獲得Date對象代表的時間是星期幾,Date類規定周日是0,周一是1,周二是2,後續的依次類推。
4、Date對象和相對時間之間的互轉
Date d3 = new Date(2009-1900,3-1,10);
long time = 1290876532190L;
//將Date類的對象轉換為相對時間
long t = d3.getTime();
System.out.println(t);
//將相對時間轉換為Date類的對象
Date d4 = new Date(time);
System.out.println(d4);
使用Date對象中的getTime方法,可以將Date類的對象轉換為相對時間,使用Date類的構造方法,可以將相對時間轉換為Date類的對象。經過轉換以後,既方便了時間的計算,也使時間顯示比較直觀了。

Ⅱ 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 date 是什麼時間

java 得到系統時間,直接私用Date類型,直接生成一個對象即可,示例如下:

1
2
3
4
5
6
7

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Date dt=new Date();//如果不需要格式,可直接用dt,dt就是當前系統時間
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//設置顯示格式
String nowTime="";
nowTime= df.format(dt);//用DateFormat的format()方法在dt中獲取並以yyyy/MM/dd HH:mm:ss格式顯示

Ⅳ 在java的程序里date類型怎麼比較大小呀

有兩個方法:

方法一:

兩個Date類型的變數可以通過compareTo方法來比較。此方法的描述是這樣的:如果參數 Date 等於此 Date,則返回值 0;如果此 Date 在 Date 參數之前,則返回小於 0 的值;如果此 Date 在 Date 參數之後,則返回大於 0 的值。

(4)javadate擴展閱讀:

Date類可以在java.util包中找到,用一個long類型的值表示一個指定的時刻。它的一個有用的構造函數是Date(),它創建一個表示創建時刻的對象。

getTime()方法返回Date對象的long值。在下面的程序中,我使用Date()構造函數創建一個表示程序運行時刻的對象,並且利用getTime()方法找到這個日期代表的毫秒數量:

import java.util.*;

public class Now {

public static void main(String[] args) {

Date now = new Date();

long nowLong = now.getTime();

System.out.println("Value is " + nowLong);

}

}

Ⅳ java,Date要怎麼輸入

首先get方法是獲取值,set方法是設置值,你這里是要設置值所以應該用set方法

其次你要set一個Date類型的值,Date類型的值有多種創建方式:

Datedate1=newDate();//當前時間
Datedate2=newDate(10000);//1970年1月1日之後10000毫秒時的時間
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Datedate3=dateFormat.parse("2018-04-0621:34:55");
Calendarcalendar=Calendar.getInstance();
calendar.set(2018,3,6,21,34,55);//這里的月份從0開始,所以填3實際表示的4月份
Datedate4=calendar.getTime();

Ⅵ java 中的date怎麼用

給你寫一個:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String s1 = sdf.format(date);//這里得到:26/03/1999 這個格式的日期
sdf = new SimpleDateFormat("HH:mm");
String s2=sdf.format(date);//這里得到的是 18:00 這個格式的時間

List<String> list = new ArrayList<String>();//然後創建一個ArrayList();
list.add(s1);
list.add(s2);//把他們添加到list
謝謝

Ⅶ 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));
}
}

Ⅷ java Date類型。

Data類型是日期類型,通常是為了獲取某些特定的日期或者轉換日期的格式為字元串。舉例:
Date date = new Date();//定義一個當前日期,此時輸出就是日期類型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//創建格式日期類型
String str = sdf.format(date);//對創建的日期進行格式化輸出。此時輸出就是字元串日期類型
輸出結果:2015--6-30 19:17:32。

Ⅸ java date格式

你好:這個是你沒保存的事,你保存下,然後關掉這個頁面,在重新進來就沒問題,他的編譯有問題

熱點內容
minecraft怎麼開伺服器地址 發布:2024-09-20 19:52:14 瀏覽:651
android彈出布局 發布:2024-09-20 19:14:29 瀏覽:981
預演算法包括 發布:2024-09-20 18:52:07 瀏覽:764
什麼數字後面跟著密碼 發布:2024-09-20 18:52:07 瀏覽:878
訂座源碼 發布:2024-09-20 18:52:06 瀏覽:383
手機mud源碼 發布:2024-09-20 18:51:28 瀏覽:940
3k我的使命腳本 發布:2024-09-20 18:11:43 瀏覽:691
建設銀行密碼怎麼設置 發布:2024-09-20 18:11:04 瀏覽:96
聚合腳本平台 發布:2024-09-20 17:51:55 瀏覽:182
訪問攔截怎麼解除安卓 發布:2024-09-20 17:28:48 瀏覽:278