简体   繁体   English

有谁知道如何提取我的日期字符串并更改格式?

[英]Does anyone know how extract my date string & change the format?

I have a valid date in my String like this: 我的字符串中有一个有效的日期,如下所示:

String strDate = "Available on 03292013";

I want to extract the date from the strDate String & change it to Available on 03/05/2015 我想从strDate字符串中提取日期并Available on 03/05/2015日将其更改为Available on 03/05/2015

Does anyone know how can I achieve this? 有谁知道我怎么能做到这一点?

You can achieve this by doing the following steps: 您可以通过执行以下步骤来实现此目的:

  • First, use the regex " [^0-9] " to extract the date from your String. 首先,使用正则表达式“ [^0-9] ”从String中提取日期。
  • Next, use the SimpleDateFormat to change the format of the extracted date from 'MMddyyyy' to 'MM/dd/yyyy' 接下来,使用SimpleDateFormat将提取日期的格式从“MMddyyyy”更改为“MM / dd / yyyy”
  • Finally, you have to append the formatted date String value to the String “Available on”. 最后,您必须将格式化的日期字符串值附加到字符串“Available on”。

Please find below code for better clarity on the implementation. 请在下面找到代码,以便更清楚地了解实施情况。

package com.stackoverflow.works;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author sarath_sivan
 */

public class DateFormatHelper {

    private static final String DD_MM_YYYY = "MMddyyyy";
    private static final String DD_SLASH_MM_SLASH_YYYY = "MM/dd/yyyy";


    public static void main(String[] args) {
        DateFormatHelper  dateFormatHelper = new DateFormatHelper();
        dateFormatHelper.run();
    }

    public void run() {
        String strDate = "Available on 03292013";
        System.out.println("Input Date: " + strDate);
        strDate = DateFormatHelper.getDate(strDate); 
        strDate = "Available on " + DateFormatHelper.formatDate(strDate);
        System.out.println("Formatted Date: " + strDate);
    }

    public static String formatDate(String strDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DD_MM_YYYY);
        Date date;
        try {   
            date = simpleDateFormat.parse(strDate);
            simpleDateFormat = new SimpleDateFormat(DD_SLASH_MM_SLASH_YYYY);
            strDate = simpleDateFormat.format(date);
         } catch (ParseException parseException) {
             parseException.printStackTrace();
         }

        return strDate;
    }

    public static String getDate(String strDate) {
        return strDate.replaceAll("[^0-9]", "");
    }

}

Output: 输出:

Input Date: Available on 03292013
Formatted Date: Available on 03/29/2013

Hope this helps... 希望这可以帮助...

Try this simple and elegant approach. 尝试这种简单而优雅的方法。

DateFormat dateParser = new SimpleDateFormat("'Available on 'MMddyyyy");
DateFormat dateFormatter = new SimpleDateFormat("'Available on 'dd/MM/yyyy");
String strDate = "Available on 03292013";
Date date = dateParser.parse(strDate);
System.out.println(dateFormatter.format(date));

This should do what you want. 这应该做你想要的。 Note that I'm just manipulating the String without any regards for what it actually contains (a date in this case). 请注意,我只是在操纵String而不考虑它实际包含的内容(在这种情况下是一个日期)。

String strDate = "Available on 03292013";
String newStr = strDate.substring(0, 15) + "/"
        + strDate.substring(15, 17) + "/" + strDate.substring(17);
System.out.println(newStr);

Result: 结果:

Available on 03/29/2013

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM