在Java中将dateTime转换为dd / mm / yy格式的日期 [英] Convert dateTime to date in dd/MM/yy format in java

查看:672
本文介绍了在Java中将dateTime转换为dd / mm / yy格式的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JODA DateTime 2012-12-31T13:32:56.483 + 13:00 。我想将其转换为 dd / MM / yy 格式的日期。因此,我希望代码返回日期-12/12/31。

I have a JODA DateTime 2012-12-31T13:32:56.483+13:00. I would like to convert it to Date in dd/MM/yy format. So I'm expecting code to return Date like - 31/12/12.

代码-

    // Input dateTime = 2012-12-31T13:32:56.483+13:00
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
    Date date = simpleDateFormat.parse(dateTime.toString("dd/MM/yy"));

结果:

Output - Mon Dec 31 00:00:00 NZDT 2012
Expected Output - 31/12/12

当我执行以下操作时,我得到了预期的输出,但是我不知道如何将其转换为Date-

When I do the following, I get the expected output but I don't know how to convert it to Date-

String string = simpleDateFormat.format(date); 

请帮助我。

Thx

编辑-我希望最终结果是dd / mm / yy格式的Util Date。我不希望String输出。我的输入是Joda DateTime yyyy-MM-ddThh:mm:ss:+ GMT。我需要将JodaDateTime转换为UtilDate。

EDIT - I want my end result to be Util Date in dd/MM/yy format. I Do not want String output. My input is Joda DateTime yyyy-MM-ddThh:mm:ss:+GMT. I need to convert JodaDateTime to UtilDate.

推荐答案

正如我最初所说,Date对象没有固有的格式。 java.util.Date 保留毫秒时间值,代表日期和日期;时间。通过选择DateFormat,可以从字符串中解析日期,也可以将日期格式化为字符串。

As I said originally, Date objects do not have an inherent format. java.util.Date holds a millisecond time value, representing both date & time. Dates are parsed from string, or formatted to string, via your choice of DateFormat.

可以按规范格式化字符串,但是后面的Date对象始终是全精度的&没有任何固有的格式概念。

The strings may be formatted per specification, but the Date objects behind them are always full precision & do not have any inherent notion of format.

要截断组合的日期和时间 java.util.Date 只是日期部分,有效地保持在午夜:

To truncate a combined "date and time" java.util.Date to just the date component, leaving it effectively at midnight:

public static Date truncateTime (Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime( date);
    cal.set( Calendar.HOUR_OF_DAY, 0);
    cal.set( Calendar.MINUTE, 0);
    cal.set( Calendar.SECOND, 0);
    cal.set( Calendar.MILLISECOND, 0);
    return cal.getTime();
}

如果您来自JodaTime DateTime ,您可以更轻松地在大部分JodaTime API中工作。

If you're coming from JodaTime DateTime, you can do this more easily working mostly in the JodaTime API.

public static Date truncateJodaDT (DateTime dt) {
    java.util.Date result = dt.toDateMidnight().toDate();
    return result;
}

希望有帮助!

请参阅:

  • http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#toDateMidnight()
  • http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractInstant.html#toDate()

现在我不确定您想要什么。您现在要使用字符串格式的日期吗?

Now I'm unsure again, what you want. You want the date in string format now?

return simpleDateFormat.format( date);    // from java.util.Date

或使用JodaTime:

Or with JodaTime:

return dateTime.toString( "dd/MM/yy");    // from org.joda.time.DateTime

这篇关于在Java中将dateTime转换为dd / mm / yy格式的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆