如何将JAVA DATE类型的日期 转换成指定格式类型的 (如:YYYY-MM-DD) 的 DATE类型数据?

如何将JAVA DATE类型的日期 转换成指定格式类型的 (如:YYYY-MM-DD) 的 DATE类型数据?,第1张

Date类型并没有格式,只有转换成String格式的时候让格式化显示。

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());

Calendar calendar = CalendargetInstance();

int year = IntegerparseInt(datetimesubstring(0,4));

int month = IntegerparseInt(datetimesubstring(5,7));

int date = IntegerparseInt(datetimesubstring(8,10));

int hour = IntegerparseInt(datetimesubstring(11,13));

int minute = IntegerparseInt(datetimesubstring(14,16));

//int second = IntegerparseInt(datetimesubstring(17,19));

if(calendarget(CalendarYEAR)>year){

int y = calendarget(CalendarYEAR)-year;

扩展资料:

Date类可以在javautil包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),创建一个表示创建时刻的对象。getTime()方法返回Date对象的long值。

import javautil;

public class Now {

public static void main(String[] args) {

Date now = new Date();

long nowLong = nowgetTime();

Systemoutprintln("Value is " + nowLong);

参考资料来源:百度百科-java日期函数

import javautilDate;
import javatextSimpleDateFormat;
class dayTime
{
public static void main(String args[])
{
Date nowTime=new Date();
Systemoutprintln(nowTime);
SimpleDateFormat time=new SimpleDateFormat("yyyy MM dd HH mm ss");
Systemoutprintln(timeformat(nowTime));
}
}

SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");

Systemoutprintln(dateFormat2format(date));

这个时间格式应该是这个吧SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ssSSS"); 要看你数据库里面的

统一处理是要满足一定条件的,比如年份必须输入四位或者年月日的顺序
不然09/5/1--->09/5/10 这个格式要当成2009年还是2010年,完全没限制的输入没办法完全兼容
给出的是你列举出来的格式,要还会有别的格式就另加处理吧~~~
public static void main(String[] args) {
//2009/05/01,09/5/1,09年5月1日,05/01/2009,2009年05月01日,20090501,2009-5-1
String date="20090501";
if(datematches("^[\\d]{4}/[\\d]{2}/[\\d]{2}$")){//2009/05/01
date=datereplace("/", "-");
}else if(datematches("^[\\d]{2}/[\\d]{1,2}/[\\d]{1,2}$")){//09/5/1
date=20+datereplace("/", "-");
}else if(datematches("^[\\d]{2}年[\\d]{1,2}月[\\d]{1,2}日$")){//09年5月1日
date=20+datereplaceAll("[年||月]", "-");
date=datesubstring(0, datelength()-1);
}else if(datematches("^[\\d]{2}/[\\d]{2}/[\\d]{4}$")){//05/01/2009
date=datesubstring(6, 10)+"-"+datesubstring(3, 5)+"-"+datesubstring(0, 2);
}else if(datematches("^[\\d]{4}年[\\d]{2}月[\\d]{2}日$")){//2009年05月01日
date=datereplaceAll("[年||月]", "-");
date=datesubstring(0, datelength()-1);
}else if(datematches("^[\\d]{8}$")){//20090501
date=datesubstring(0, 4)+"-"+datesubstring(4, 6)+"-"+datesubstring(6, 8);
}else if(datematches("^[\\d]{4}-[\\d]{1,2}-[\\d]{1,2}$")){//2009-5-1
}
javasqlDate d=javasqlDatevalueOf(date);//不直接输出是因为要考虑2009-5-1和2009-5-11日期和月份两位数的处理
Systemoutprintln(dtoString());
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13209319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-20
下一篇 2023-06-20

发表评论

登录后才能评论

评论列表(0条)

保存