【问题标题】:how to convert date in java如何在java中转换日期
【发布时间】:2011-12-12 11:45:45
【问题描述】:

我是安卓新手。 我必须将以下日期转换为这个时间戳(Wed Oct 12 14:17:42 GMT+05:30 2011

Thu, 27 May 2010 12:37:27 GMT

这是我通过标头从服务器获取的日期。我已将其转换为 String 对象。现在我必须将其转换为日期格式,例如:Wed Oct 12 14:17:42 GMT+05:30 2011

请您帮我如何使用时间戳将其转换为 (Wed Oct 12 14:17:42 GMT+05:30 2011) 这种格式。

【问题讨论】:

    标签: java datetime date timestamp


    【解决方案1】:

    查看DateFormatSimpleDateFormat,它们提供了parse()format() 方法。 DateFormat 提供了一堆标准格式,而SimpleDateFormat 允许您提供自己的格式表达式。

    输入日期示例:

    //note that you need the locale as well, since the weekdays and months are written in a specific language, English in that case
    SimpleDateFormat parseFormat = new SimpleDateFormat( "E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH );
    SimpleDateFormat writeFormat = new SimpleDateFormat( "E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH );
    writeFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
    
    Date tDate = parseFormat.parse( "Wed, 12 Oct 2011 14:17:42 GMT" );
    
    System.out.println(writeFormat.format(tDate )); //Wed Oct 12 14:17:42 GMT 2011
    

    编辑:如果您想要更实用的 API,请尝试 JodaTime

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 2012-02-13
      • 2012-08-12
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      相关资源
      最近更新 更多