本文目錄一覽:
- 1、在java中,String轉換Date日期類型容易出現時間轉換異常,如何避免!
- 2、java獲取的日期只顯示年月日不顯示時分秒,怎麼處理
- 3、發現一個java日期的 bug
- 4、JAVA 日期格式化錯誤…這是什麼原因啊???
- 5、java中的Date類為什麼很多方法被廢棄了
- 6、java Calendar日期類 和異常的問題
在java中,String轉換Date日期類型容易出現時間轉換異常,如何避免!
public static Date valueOf(String s)將 JDBC 日期轉義形式的字元串轉換成 Date 值。
參數:
s – 表示 “yyyy-mm-dd” 形式的日期的 String 對象
返回:
表示給定日期的 java.sql.Date 對象
java獲取的日期只顯示年月日不顯示時分秒,怎麼處理
可以使用java中的日期格式化工具java.text.SimpleDateFormat來格式化,以下為示例代碼:
1.創建java.text.SimpleDateFormat實例
java.text.SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”, Locale.getDefault());
參數說明,”yyyy-MM-dd”表示日期的格式,也可以是這樣的 yyyy-MM-dd HH:mm:ss注意大小寫
2.將Date對象轉成字元串
Date date = new Date();
String str = sdf.format(date);
System.out.println(str);
3.將字元串轉成Date
String str = “2013-12-12”;
Date date = sdf.parse(str);
System.out.prinltn(date);
注意,SimpleDateFormat解析工具,指定的format以後,只能解析指定格式的日期,例如指定了yyyy-MM-dd則不可以再去解析 yyyy-MM-dd HH:mm:ss格式。
發現一個java日期的 bug
sql2000默認時間是1900-1-1 0:00:00
1928年元旦前幾分鐘,國民政府北伐成功,民國變了時制,從地方時間開始接軌國際時間,一下跳了5分鐘。導致今天的java時區上反映了這一變化?正解么
JAVA 日期格式化錯誤…這是什麼原因啊???
是你的yyyy-mm-dd,錯了。應該是:yyyy-MM-dd
mm指的是分鐘,MM指的是月份,附上格式中的模式字母:
字母 日期或時間元素 表示 示例
G Era 標誌符 Text AD
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周數 Number 27
W 月份中的周數 Number 2
D 年中的天數 Number 189
d 月份中的天數 Number 10
F 月份中的星期 Number 2
E 星期中的天數 Text Tuesday; Tue
a Am/pm 標記 Text PM
H 一天中的小時數(0-23) Number 0
k 一天中的小時數(1-24) Number 24
K am/pm 中的小時數(0-11) Number 0
h am/pm 中的小時數(1-12) Number 12
m 小時中的分鐘數 Number 30
s 分鐘中的秒數 Number 55
S 毫秒數 Number 978
z 時區 General time zone Pacific Standard Time; PST; GMT-08:00
Z 時區 RFC 822 time zone -0800
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 Calendar日期類 和異常的問題
因為Java認為周日是第一天,因此會比你認為的多1
運行時異常就是在程序執行過程中出現的異常,這種異常編寫代碼時可以通過程序捕捉,也可以不進行人為的處理。
例如:空指向異常(NullPointerException),這種異常一般都不會人為處理,但執行過程中可以會出現。
非運行時異常也稱為檢查異常,在編寫代碼時就必須對異常進行處理,否則編譯時會提示錯誤,這種異常也是必須人為處理的。(try…catch或throws)
例如:IOException或SQLException,這類異常在編寫時就必須處理。
原創文章,作者:VXRD4,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130065.html