首页 > 编程知识 正文

包含java中date日期格式化的词条

时间:2023-12-29 13:16:39 阅读:329943 作者:YWMW

本文目录一览:

java中,Date如何格式化为“yyyy-MM-dd”格式Date,并可按需求格式输出!(java.util.Date)

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

System.out.println(dateFormat2.format(date));

java中的Date怎么转换成YYYYMMDD形式的

SimpleDateFormat inSdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);

创建SimpleDateFormat对象的时候使用带Locale的构造参数

因为你的星期和月份是用E文写的 所以parse回来的时候自然得用E文的Locale 用默认中文的Locale就会认不出来

btw 你用US的Locale的话 时区也会跑那里去 所以你应该给时区指定GMT+08:00 而不是光一个GMT

参考这段小代码

String s ="Thu, 02 Mar 2006 05:14:25 GMT+08:00";

SimpleDateFormat inSdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);

SimpleDateFormat outSdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.CHINA);

try {

Date dateS = inSdf.parse(s);

System.out.println(outSdf.format(dateS));

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//1、定义转换格式

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

SimpleDateFormat formatter2  = new SimpleDateFormat("yyyyMMdd");

//2、调用formatter2.parse(),将"19570323"转化为date类型  输出为:Sat Mar 23 00:00:00 GMT+08:00 1957

Date  date = formatter2.parse(dateString);

//3、将date类型  (Sat Mar 23 00:00:00 GMT+08:00 1957)转化为String类型

//注意现在用的是formatter来做转换,输出为String类型的:"1957-03-23"

String  dString = formatter.format(date);

//4、将String转化为date,需要注意java.sql.Date.valueOf()函数只能接受参数类型为yyyy-MM-dd类型的

Date data = java.sql.Date.valueOf(dString);

//5、将获取的date类型的出生日期赋值给javabean

personAudit.setBirthDate((emp.getHealthCarePrincipalPerson() != null

emp.getHealthCarePrincipalPerson().getBirthTime() != null)?data:null)。

java日期格式化问题?

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");x0dx0aDate currentTime = new Date();// 得到当前系统时间x0dx0aString str_date = formatter.format(currentTime); // 将日期时间格式化x0dx0aSystem.out.print(str_date );x0dx0a这样肯定没问题啊

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

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

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

Calendar calendar = Calendar.getInstance();

int year = Integer.parseInt(datetime.substring(0,4));

int month = Integer.parseInt(datetime.substring(5,7));

int date = Integer.parseInt(datetime.substring(8,10));

int hour = Integer.parseInt(datetime.substring(11,13));

int minute = Integer.parseInt(datetime.substring(14,16));

//int second = Integer.parseInt(datetime.substring(17,19));

if(calendar.get(Calendar.YEAR)year){

int y = calendar.get(Calendar.YEAR)-year;

扩展资料:

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

import java.util.*;

public class Now {

public static void main(String[] args) {

Date now = new Date();

long nowLong = now.getTime();

System.out.println("Value is " + nowLong);

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。