本文介绍了使用Java的日期格式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期/时间值为 2010-07-26T11:37:52Z
现在我想要在 26-jul-2010(dd-mon -yyyy)格式,我该怎么办?

解决方案

构造两个SimpleDateFormat对象。第一个解析()值到Date对象,第二个用于将Date对象重新转换成字符串,例如

  try {
DateFormat df1 = new SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ss'Z);
DateFormat df2 = new SimpleDateFormat(dd-MMM-yyyy);
return df2.format(df1.parse(input));
}
catch(ParseException e){
return null;
}

解析可能会抛出一个ParseException,所以你需要抓住并处理它。 / p>

I am having a date/time value as 2010-07-26T11:37:52Z ,now I want date in 26-jul-2010 (dd-mon-yyyy) format, how do I do it?

解决方案

Construct two SimpleDateFormat objects. The first you parse() the value from into a Date object, the second you use to turn the Date object back into a string, e.g.

try {
  DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  DateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy");
  return df2.format(df1.parse(input));
}
catch (ParseException e) {
  return null;
}

Parsing can throw a ParseException so you would need to catch and handle that.

这篇关于使用Java的日期格式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:39