当前位置:首页 » 编程语言 » 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 20:17:28 浏览:814
幼儿速算法 发布:2025-05-15 20:15:08 浏览:86
best把枪密码多少 发布:2025-05-15 20:13:42 浏览:548
android安装程序 发布:2025-05-15 20:13:20 浏览:559
c语言跳出死循环 发布:2025-05-15 20:06:04 浏览:824
a19处理器相当于安卓哪个水平 发布:2025-05-15 20:05:29 浏览:639
荣耀9i安卓强行关机按哪个键 发布:2025-05-15 20:00:32 浏览:750
密码锁写什么最好 发布:2025-05-15 19:05:31 浏览:782
5的源码是 发布:2025-05-15 19:04:07 浏览:719
c语言创建的源文件 发布:2025-05-15 18:54:08 浏览:611