導航:首頁 > 編程語言 > javasql日期

javasql日期

發布時間:2023-08-29 02:11:48

❶ 如何正確比較日期 java.sql.Date

java.sql.Date比較:
import java.sql.Date;
例如今天是2010-12-2
Date d1 = new Date(System.currentTimeMili());
Date d2 = new Date(System.currentTimeMili()+1);//比d1晚1毫秒
日期上,我們認為d1和d2是相等的
但是
System.out.println(d1.before(d2));
輸出結果是true;
其實我們希望看到的是這兩個對象在日期上是相等的。
因為我們只關心「日期」,而「2010-12-2」不等於「2010-12-2」
這個結果顯然是我們所不能接受的。
究其原因,是因為Date內封裝了一個精確到毫秒的表示時間的
private transient long fastTime;
而before和after的函數的實現如下,都是判斷fastTime的值,所以達不到我們只比較日期的要求。
public boolean before(Date when) {
return getMillisOf(this) < getMillisOf(when);
}
public boolean after(Date when) {
return getMillisOf(this) > getMillisOf(when);
}
把日期格式成標準的「年月日」,然後對格式化後的對象進行比較,得到比較的結果
本文給出一種「格式成標准化」的方式
Date d1_temp = java.sql.Date.valueOf(d1.toString());
Date d2_temp = java.sql.Date.valueOf(d2.toString());
System.out.prinltn(d1_temp.equals(d2_temp));//輸出結果是true;
System.out.prinltn(d1_temp.before(d2_temp));//輸出結果是false;
System.out.prinltn(d1_temp.after(d2_temp));//輸出結果是false;
需要邏輯的話,可以寫成
if(d1_temp.before(d2_temp)){
.........
}

❷ Java如何獲取Date類型且格式為yyyy-mm-dd的日期數據

@return返回長時間格式 yyyy-MM-dd HH:mm:ss

*/ public static Date getSqlDate() {

Date sqlDate = new java.sql.Date(new Date().getTime());

return sqlDate; }

/**

* 獲取現在時間

@return返回長時間格式 yyyy-MM-dd HH:mm:ss

*/ public static Date getNowDate() {

Date currentTime = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateString = formatter.format(currentTime);

ParsePosition pos = new ParsePosition(8);

java.sql 類 Date

java.lang.Object

java.util.Date

java.sql.Date

所有已實現的介面:

Serializable,Cloneable,Comparable<Date>

public class Dateextends Date

概述:一個包裝了毫秒值的瘦包裝器 (thin wrapper),它允許 JDBC 將毫秒值標識為 SQL DATE 值。毫秒值表示自 1970 年 1 月 1 日 00:00:00 GMT 以來經過的毫秒數。

為了與 SQL DATE 的定義一致,由 java.sql.Date 實例包裝的毫秒值必須通過將小時、分鍾、秒和毫秒設置為與該實例相關的特定時區中的零來「規范化」。

以上內容參考:網路-date

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

//得到long類型當前時間

longl=System.currentTimeMillis();

//new日期對

Datedate=newDate(l);

//轉換提日期輸出格式

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-

ddHH:mm:ss");System.out.println(dateFormat.format(date));

(3)javasql日期擴展閱讀

package com.ob;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateTest {

public static void main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance();

System.out.println("年: " + now.get(Calendar.YEAR));

System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");

System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));

System.out.println("時: " + now.get(Calendar.HOUR_OF_DAY));

System.out.println("分: " + now.get(Calendar.MINUTE));

System.out.println("秒: " + now.get(Calendar.SECOND));

System.out.println("當前時間毫秒數:" + now.getTimeInMillis());

System.out.println(now.getTime());

Date d = new Date();

System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);

System.out.println("格式化後的日期:" + dateNowStr);

String str = "2012-1-13 17:26:33";

//要跟上面sdf定義的格式一樣
Date today = sdf.parse(str);

System.out.println("字元串轉成日期:" + today);
}
}

閱讀全文

與javasql日期相關的資料

熱點內容
演算法戰書籍 瀏覽:575
卸載網路伺服器是什麼意思 瀏覽:123
菜鳥app的收貨地址在哪裡 瀏覽:488
伺服器配什麼顯卡 瀏覽:369
動態壁紙不動了是怎麼回事安卓 瀏覽:412
申萬宏源app哪裡看總盈利 瀏覽:133
單片機測電感電容 瀏覽:165
android在子線程中更新ui 瀏覽:694
演算法分析師面試有什麼要求 瀏覽:994
容器演算法大全圖解 瀏覽:69
cad後置命令失效 瀏覽:692
殺手阻擊存檔文件夾是哪一個 瀏覽:212
禁書pdf 瀏覽:920
沒用app語音智能提醒怎麼設置 瀏覽:502
linuxwiki安裝 瀏覽:680
隔牆演算法 瀏覽:174
安卓手機為什麼app不通知 瀏覽:550
申請雲伺服器購買費用 瀏覽:115
雲伺服器鏡像下載到本地 瀏覽:4
電腦文件夾名有橫杠 瀏覽:154