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

javatodate

發布時間: 2022-05-18 18:26:34

java string轉化成date的問題

Date型的存在資料庫里是什麼樣都行
當你要讀資料庫進行賽選的時候,可以用to_char(欄位名,'yyyy-mm-dd')就可以得到你想要的格式了
如:select * from t where to_char(DateVauel,'yyyy-mm-dd')<'2008-08-08' ;DateVauel是一個日期型的欄位
上面的sql就可以選出所有的早於2008-08-08的記錄了

Ⅱ java 兩個時間段計算

兩個時間段四個時間點,相當於時間軸上的兩條線段(b代表起點,e代表端點,b<=e)和4個端點。
可分3種情況:
1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此時,重合天數為零。
2.相交。
情況一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
情況二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
3.包含:計算較短的時間段日期長度。
(b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)

實現代碼如下:

[java] view plain
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* @author skysnow
*
*/
public class myDateUtil {
/**
*這里共有2個時間段(b1-----e1)【b2-----e2】,4個時間點;
*相當於兩條線段(b代表起點,e代表端點,b<=e),4個端點。
*可分3種情況:
*1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此時,重合天數為零。
*2.相交。
*情況一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
*情況二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
*3.包含:計算較短的時間段日期長度。
*(b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
*【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
* @param begindate1 開始日期
* @param enddate1 結束日期
* @param begindate2開始日期
* @param enddate2 結束日期
* @return
*/
public static String getDayCoincidence(Date begindate1,Date enddate1,Date begindate2,Date enddate2){
long b1=begindate1.getTime();
long e1=enddate1.getTime();
long b2=begindate2.getTime();
long e2=enddate2.getTime();
assert(b1<e1&&b2<e2);
String coincidenceday;
if(b1<=b2&&e1>=e2){//(b1---【b2-----e2】--e1)
System.out.println("1包含2");
coincidenceday=getDayDifference(enddate2,begindate2);
}else if(b1>=b2&&e1<=e2){//【b2---(b1-----e1)--e2】
System.out.println("2包含1");
coincidenceday=getDayDifference(enddate1,begindate1);
}else if(b1>=b2&&b1<=e2&&e2<=e1){//【b2---(b1---e2】----e1)
System.out.println("相交");
coincidenceday=getDayDifference(enddate2,begindate1);
}else if(b1<=b2&&e1<=e2&&e1>=b2){//(b1---【b2---e1)----e2】
System.out.println("相交");
coincidenceday=getDayDifference(enddate1,begindate2);
}else if(e1<=b2||b1>=e2){
coincidenceday="0";
}else{
coincidenceday="";
System.out.println("意料外的日期組合,無法計算重合天數!");
}
System.out.println("重合天數為["+coincidenceday+"]天。");
return coincidenceday;
}
/**
* 計算兩個日期的相差天數(d1-d2)
* @param d1
* @param d2
* @return
*/
public static String getDayDifference(Date d1,Date d2){
StringBuffer ds = new StringBuffer();
try{
long num = (d1.getTime()-d2.getTime())/1000;
long days = num/(3600*24);
if(days>=0)ds.append(days);
}catch(Exception e){
ds=new StringBuffer("");
e.printStackTrace();
}
return ds.toString();
}

public static Date stringToDate(String strDate) {
if (strDate==null){return null;}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static String getThisMonth()
{
// 本月的第一天
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DATE, 1);
SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
String fd = simpleFormate.format(calendar.getTime());

// 本月的最後一天
calendar.set( Calendar.DATE, 1 );
calendar.roll(Calendar.DATE, - 1 );
String ld = simpleFormate.format(calendar.getTime());
return fd+","+ld;
}
public static void main(String[] args) {
String[] thisMonth=getThisMonth().split(",");
Date begindate1 = stringToDate(thisMonth[0]+" 00:05:00");
Date enddate1 = stringToDate(thisMonth[0]+" 24:05:00");;
Date begindate2 = stringToDate(thisMonth[0]+" 00:05:00");
Date enddate2 = stringToDate(thisMonth[1]+" 00:00:00");
System.out.println(getDayCoincidence(begindate1, enddate1, begindate2, enddate2));
}
}

Ⅲ 怎麼把String 變成 Date類型 JAVA String to date

使用SimpleDateFormat進行格式化
DateFormat format = new SimpleDateFormat("yyyyMMdd");//裡面的參數可以自定義需要格式化的類型
String str = "201508";
System.out.println(format.parse(str));

Date轉成String
System.out.println(format.format(new Date));

Ⅳ java中將String轉成Date

寫了一段代碼,不知道是否合樓主的意..

能夠將ddMMM這種形式的日期,比如25JUL轉化為MM-dd的日期格式..

順說:jul是七月..

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SimpleDatePrint {
public static void main(String[] args) {
try {
// 設定接收25JUL的日期格式
DateFormat df1 = new SimpleDateFormat("ddMMM", Locale.US);
// 將接收到的字元串轉化為Date類型
Date date = df1.parse("25JUL");
// 設定輸入的日期格式
DateFormat df2 = new SimpleDateFormat("MM-dd");
// 按格式生成輸入結果
String result = df2.format(date);
// 列印結果
System.err.println(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Ⅳ java中如何將SimpleDateFormat類型轉換成Date類型

public class SimpleDateFormatDemo {

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

// TODO Auto-generated method stub

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//Date指定格式:yyyy-MM-dd HH:mm:ss:SSS

Date date = new Date();//創建一個date對象保存當前時間

String dateStr = simpleDateFormat.format(date);//format()方法將Date轉換成指定格式的String

System.out.println(dateStr);//2018-08-24 15:37:47:033

String string = "2018-8-24 12:50:20:545";

Date date2 = simpleDateFormat.parse(string);//調用parse()方法時 注意 傳入的格式必須符合simpleDateFormat對象的格式,即"yyyy-MM-dd HH:mm:ss:SSS" 否則會報錯!!

System.out.println(date2);//Fri Aug 24 12:50:20 CST 2018

}

}


(5)javatodate擴展閱讀

public class StringToDate

{

public final static java.sql.Date string2Date(String dateString) throws java.lang.Exception

{

DateFormat dateFormat;

dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);

dateFormat.setLenient(false);

java.util.Date timeDate = dateFormat.parse(dateString);//util類型

java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql類型

return dateTime;

}

}

Ⅵ 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 date類parseException問題

1、報錯原因只有一個,即你的fromDate,toDate字元串沒有按「MM/dd/yyyy」這么串,才會報這個錯。

2、估計你是用了myDate97或是jquery的date控制項才這么轉,再看下你的js代碼中對這個值的格式的規定,看看是這個「MM/dd/yyyy"的格式否。
再debug到你的這段代碼處,看下兩個字元串的格式,絕對可以解決你的問題了。

Ⅷ java怎麼計算出某年某月到某年某月之間的總月數並顯示出來

天數好算,月數不好算,因為有28天,30天,31天,偶爾還有29天一個月的,告訴你怎麼算天數吧
public static java.util.Date toDate(String dateStr) {
Date d=null;
SimpleDateFormat formater=new SimpleDateFormat("yyyy/MM/dd");
try {
formater.setLenient(false);
d=formater.parse(dateStr);
} catch(Exception e) {
d=null;
}
return d;
}
public static long dayDiff(Date date1, Date date2) {
return (date2.getTime() - date1.getTime()) / 86400000;
}
public static void main(String[] args) {
System.out.println("請輸開始日期如:2013/03/15");
Scanner sc=new Scanner(System.in);
String dateStr1=sc.nextLine();
Date date1=toDate(dateStr1);
System.out.println("請輸結束日期如:2013/03/15");
String dateStr2=sc.nextLine();
Date date2=toDate(dateStr2);
Long day=dayDiff(date1,date2);
System.out.println("相差"+day+"天");
}

Ⅸ java中如何將任何形式的String轉成Date

我曾經寫過一個類,專門用作數字時間的轉化,現在發給你

package cn.siyu.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StringToDateCoversion {
private static List<String> list = new ArrayList<String>();

/*public void setList(List<String> list) {
this.list = list;
}*/
private List<String> getList() {
return list;
}
private StringToDateCoversion(List<String> list) {
this.list=list;
}

public StringToDateCoversion(String list) {
this.list.add(list);
}
public String DateToStringFormat() throws Exception{
SimpleDateFormat format= new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
return format.format(getDate());
}

/**
*
* the date as follows can conversion(all the character be transform,even mixed-use except the last one):<br>
* 2012-10-10 05-30-30 12-10-10 05-30-30<br>
* 2012/10/10 05/30/30 12/10/10 05/30/30<br>
* 2012|10|10 05|30|30 12|10|10 05|30|30<br>
* 2012.10.10 05.30.30 12.10.10 05.30.30<br>
* 2012 10 10 05 30 30 12 10 10 05 30 30<br>
* 2012,10,10,05,30,30 12,10,10,05,30,30<br>
* 20121010053030 121010053030 <br>
*
* so as can contains the hours ,minutes,second<br>
*
* try to format the string to data,<br>
* if the year>1000,will as it.example:1870-10-10 --->1870-10-10<br>
* if 65 < year < 100,the year will be add "19". example:70-10-10 --->1970-10-10<br>
* if 00 < year < 40,the year will be add "20" . example:10-10-10 --->2010-10-10<br>
* @throws ParseException
* */
public Date getDate() throws ParseException{
//StringToDateCoversion date = new StringToDateCoversion("2012,10/10");
StringToDateCoversion splitList = this.splitList("-").splitList("/").splitList(" ").splitList(",").splitList("\\.").splitList("\\|");
List<String> date = splitList.getList();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String date1=null;
Date retuendate=null;
/**
* deal with 20121010 121010
* */
if(date.size()>1){
date1 = ListtoString(date);
retuendate = format.parse(date1);
}
if(date.size()==1){
/**
* if the string not eval ,should add zero to the first position
* */
if(date.get(0).length()%2==1){
date.set(0, "0"+date.get(0));
}
date1 = ListtoString(getList(subList(date, 2)));
retuendate = format.parse(date1);
}

return retuendate;
}
private List<String> subList(List<String> date,int i){
List<String> returnDate = new ArrayList<String>();
String string = date.get(0);
returnDate.add(string.substring(0, i));
for(;i<string.length();i=i+2){
returnDate.add(string.substring(i, i+2));
}
return returnDate;
}
private List<String> getList(List<String> date){
int size = date.size();
Integer first =Integer.parseInt(date.get(0));
Integer second =Integer.parseInt(date.get(1));
if(second>12||size==4||size==7){
for (int i = 0; i < size-1; i++) {
String infor=i==0?date.get(i):"";
date.set(i,(infor+date.get(i+1)));
}
date.remove(size-1);
return date;
}else if(first<=40){
date.set(0, "20"+date.get(0));
}else if(first>40){
date.set(0, "19"+date.get(0));
}
return date;
}
private String ListtoString(List<String> date){
String returnDate=null;

Integer first = Integer.parseInt(date.get(0));
if(first>100){
returnDate=first.toString();
}else if(first<=50&&first>=0){
returnDate="20"+first.toString();
}else if(first<100&&first>50){
returnDate="19"+first.toString();
}
for(int i=1;i<6;i++){
if(date.size()<=i){
returnDate=returnDate+"-00";
}else{
returnDate=returnDate+"-"+date.get(i);
}
}
return returnDate;
}

private StringToDateCoversion splitList(String regex){
if(list==null®ex==null){ // 這個地方的代碼看好,是if(list==null & regex ==null),提交的時候總是被轉義,你自己修改一下

return null;
}
List<String> returnlist = new ArrayList<String>();
Object[] array = list.toArray();
for (Object Obj : array) {
String[] splitList = Obj.toString().split(regex);
for (String string : splitList) {
returnlist.add(string);
}
}
return new StringToDateCoversion(returnlist);
}
}

Ⅹ java如何轉換日期格式

import java.util.*;
import java.text.*;
import java.util.Calendar;

public class VeDate {
/**
* 獲取現在時間
*
* @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);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}

/**
* 獲取現在時間
*
* @return返回短時間格式 yyyy-MM-dd
*/
public static Date getNowDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}

/**
* 獲取現在時間
*
* @return返回字元串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}

/**
* 獲取現在時間
*
* @return 返回短時間字元串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}

/**
* 獲取時間 小時:分;秒 HH:mm:ss
*
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date currentTime = new Date();
String dateString = formatter.format(currentTime);
return dateString;
}

/**
* 將長時間格式字元串轉換為時間 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}

/**
* 將長時間格式時間轉換為字元串 yyyy-MM-dd HH:mm:ss
*
* @param dateDate
* @return
*/
public static String dateToStrLong(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}

/**
* 將短時間格式時間轉換為字元串 yyyy-MM-dd
*
* @param dateDate
* @param k
* @return
*/
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}

/**
* 將短時間格式字元串轉換為時間 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}

/**
* 得到現在時間
*
* @return
*/
public static Date getNow() {
Date currentTime = new Date();
return currentTime;
}

/**
* 提取一個月中的最後一天
*
* @param day
* @return
*/
public static Date getLastDate(long day) {
Date date = new Date();
long date_3_hm = date.getTime() - 3600000 * 34 * day;
Date date_3_hm_date = new Date(date_3_hm);
return date_3_hm_date;
}

/**
* 得到現在時間
*
* @return 字元串 yyyyMMdd HHmmss
*/
public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}

/**
* 得到現在小時
*/
public static String getHour() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String hour;
hour = dateString.substring(11, 13);
return hour;
}

/**
* 得到現在分鍾
*
* @return
*/
public static String getTime() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String min;
min = dateString.substring(14, 16);
return min;
}

/**
* 根據用戶傳入的時間表示格式,返回當前時間的格式 如果是yyyyMMdd,注意字母y不能大寫。
*
* @param sformat
* yyyyMMddhhmmss
* @return
*/
public static String getUserDate(String sformat) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(sformat);
String dateString = formatter.format(currentTime);
return dateString;
}

/**
* 二個小時時間間的差值,必須保證二個時間都是"HH:MM"的格式,返回字元型的分鍾
*/
public static String getTwoHour(String st1, String st2) {
String[] kk = null;
String[] jj = null;
kk = st1.split(":");
jj = st2.split(":");
if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
return "0";
else {
double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
if ((y - u) > 0)
return y - u + "";
else
return "0";
}
}

/**
* 得到二個日期間的間隔天數
*/
public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
java.util.Date date = myFormatter.parse(sj1);
java.util.Date mydate = myFormatter.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}

/**
* 時間前推或後推分鍾,其中JJ表示分鍾.
*/
public static String getPreTime(String sj1, String jj) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mydate1 = "";
try {
Date date1 = format.parse(sj1);
long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
date1.setTime(Time * 1000);
mydate1 = format.format(date1);
} catch (Exception e) {
}
return mydate1;
}

熱點內容
超凡先鋒配置不行怎麼辦 發布:2025-05-15 23:27:54 瀏覽:530
win7取消加密 發布:2025-05-15 23:26:37 瀏覽:470
不用internet打開ftp 發布:2025-05-15 23:06:00 瀏覽:153
sql字元串取數字 發布:2025-05-15 22:57:45 瀏覽:124
推薦編程課 發布:2025-05-15 22:34:12 瀏覽:618
表拒絕訪問 發布:2025-05-15 22:29:37 瀏覽:978
電腦怎樣解壓文件 發布:2025-05-15 22:25:32 瀏覽:439
dns伺服器怎麼看 發布:2025-05-15 22:17:27 瀏覽:151
3dm的壓縮包 發布:2025-05-15 22:09:23 瀏覽:662
和存儲字長 發布:2025-05-15 21:54:09 瀏覽:515