简体   繁体   English

如何将字符串转换为日期,然后转换为其他格式?

[英]How can I convert a String to a Date and then to a different format?

Actually I wanted to convert one String to date and then I need to format it in to one another format.. 实际上,我想将一个String转换为日期,然后将其格式化为另一种格式。

The String I have is 我有的字符串是

 String val="Wed Jan 08 08:49:13 GMT+05:30 2014";

To convert it to 转换成

 2014-01-30 10:14:18  , this format

Is it possible to do this I tried some method like 是否可以做到这一点,我尝试了一些方法,例如

       SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = df.format(c.getTime());

        Calendar cal =   Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
          // Date currentLocalTime = ;
            DateFormat date = new SimpleDateFormat("HH:mm:ss:mmss a"); 

But nothing worked 但是没有任何效果

Your format string for parsing doesn't match the example date you have given. 您用于解析的格式字符串与您指定的示例日期不匹配。

You could try something like the following: 您可以尝试以下操作:

    String val = "Wed Jan 08 08:49:13 GMT+05:30 2014";
    DateFormat inFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
    Date date = inFmt.parse(val);
    DateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    outFmt.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
    System.out.println(outFmt.format(date));

Note the following correspondences in the first two lines: 请注意前两行中的以下对应关系:

  • Wed -> EEE 星期三-> EEE
  • Jan -> MMM 一月-> MMM
  • 08 -> dd 08-> dd
  • etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 SQL 日期转换为不同格式的字符串日期? - How do I convert a SQL date to String date in different format? 如何使用休眠注释将日期字符串以yyyy-MM格式转换为日期 - How Can I convert Date String as format yyyy-MM to Date with hibernate annoation 如何解析“ddMM”格式的日期(以字符串形式提供)并将其转换为 Java 中的日期对象? - How can I parse a date (provided as a string) in the "ddMM" format and convert it to a Date Object in Java? 如何将日期字符串转换为自定义日期格式? - How do I convert my date string to a custom date format? 如何将字符串格式的给定日期视为 est 并将其转换为 java 中的 utc? - How can I consider given date in string format as est and convert it to utc in java? 如何将以字符串形式存储的日期解析为Java中的其他格式? - how can I parse my date stored as a String to a different format in Java? 如何将文字字符串值转换为日期格式? - How do I convert a literal string value to date format? 无法将字符串转换为正确的日期格式 - Can not convert a string to proper date format 将日期转换成其他格式 - convert date into different format 我想将字符串转换为Java 6中的日期格式 - I want to convert a string to date format in java 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM