Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用字符串将日期从数字格式转换/替换为EEEE、MMMM dd、yyyy格式_Java_Regex_String_Date_Simpledateformat - Fatal编程技术网

Java 使用字符串将日期从数字格式转换/替换为EEEE、MMMM dd、yyyy格式

Java 使用字符串将日期从数字格式转换/替换为EEEE、MMMM dd、yyyy格式,java,regex,string,date,simpledateformat,Java,Regex,String,Date,Simpledateformat,我正在尝试执行以下操作:创建一个类,将日期从数字格式转换为“EEEE,MMMM dd,yyyy”格式,而不影响包含日期的字符串的其余部分 例如,“78年9月6日,发生了一件非常酷的事情……”(每句话可能有不止一个日期,甚至是不正确的日期,如“88年31月31日”或“11/&&&/00”以及“1988年9月6日”。)应该变成“1978年9月6日,星期三,发生了一件非常酷的事情……”我应该忽略无效日期或没有年份的日期。请注意,与上述示例类似的句子保存在一个文件中;此文件中每行可能有一个或多个句子;这

我正在尝试执行以下操作:创建一个类,将日期从数字格式转换为“EEEE,MMMM dd,yyyy”格式,而不影响包含日期的字符串的其余部分

例如,“78年9月6日,发生了一件非常酷的事情……”(每句话可能有不止一个日期,甚至是不正确的日期,如“88年31月31日”或“11/&&&/00”以及“1988年9月6日”。)应该变成“1978年9月6日,星期三,发生了一件非常酷的事情……”我应该忽略无效日期或没有年份的日期。请注意,与上述示例类似的句子保存在一个文件中;此文件中每行可能有一个或多个句子;这些日期可以显示每行多个日期;日期不详,;并且可能会有一行无效的日期。(我希望这能说明我需要做什么。(顺便说一句,正如我在下面解释的那样,我已经知道如何使用“SimpleDataFormat”将单个有效数字日期转换为“EEEE,MMMM dd,yyyy”格式,但问题是要解析此文件中的每一行,找到一个或多个日期,(如果日期有效,则转换它),然后用新格式替换数字日期。我希望现在清楚了!)

我已经通过使用“SimpleDataFormat”解决了从简单的单个数字日期到“EEEE,MMMM dd,yyyy”格式的转换问题,但我对使用正则表达式解析字符串语句、查找日期并用新格式替换它感到困惑(我没有列出完整的解决方案,因为它很长)

现在,我一直在尝试找到一个应该有效的解决方案,但由于某些原因它不起作用。请参阅下一个代码:

String line = "We landed on the moon on 7/20/1969. 7/4/2012 is the Fourth of July.";

// I expect to see "7/20/1969" and "7/20/2012" as output but the following does not
// work:

dateRegex = "^([1-9]|0[1-9]|1[012])/([1-9]|0[1-9]|[1-2][0-9]|3[01])/(\\d\\d)|(19|20)\\d\\d$";
Pattern p = Pattern.compile(dateRegex);
Matcher m = p.matcher(line);
System.out.println(line);

while(m.find()) {
    System.out.println("Found " + p + " at: " + m.start());
    // return the sequences characters matching "dateRegex (or so it should...?)
    System.out.println("Match : " + m.group())  // it does not print 'cause nothing is found
}
// the above code works find with a simpler regex string variable, but nothing like this
// what's worng here?   Is this a wrong "dateRegex" pattern?  Thanks.
如果您能为我提供经验证的结果,我将不胜感激。谢谢。

使用String.Format()

当然,这个例子使用了非常优秀的Joda时间库,您也应该这样做

更新: 在正确理解了这个小示例的问题(我认为)之后,使用标准Java库应该可以让您开始了

package com.company;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        String example = "On 9/6/78, a really cool thing happened...";
        SimpleDateFormat replacement = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
        Pattern datePattern = Pattern.compile("\\b\\w{1,2}/\\w{1,2}/(?:\\w{4}|\\w{2})\\b");
        Date now = new Date();
        System.out.println(datePattern.matcher(example).replaceAll(replacement.format(now)));
    }
}

谢谢,我将尝试这个解决方案,但在本练习中,我被迫使用“SimpleDataFormat”这是一个要求。顺便说一句,我忘了解释这些句子可以有多个日期实例,我不知道或不应该知道这些句子是什么,因为所有的句子都保存在一个文件中。再次感谢,我会看看我能对你的答案做些什么。好的。它也适用于SimpleDateFormat。我不确定我是否理解然后你要做什么。你在解析一个文件来替换日期吗?已知日期的数量吗?确定替换哪个日期的机制是什么?假设我有以下文本:“等等等等等等等等等等等等日期”,日期应该替换为相同的日期?还是不同的日期?您的语句的语法似乎根本不正确。首先,DateFormatter类(哪个库)在哪里?我只能通过“import javax.swing.text.DateFormatter”和“print()”找到它不受支持。另一件事是,您使用两种方法(一种是在under之后)进行的构建似乎很奇怪,也不寻常。回答您的一些问题:它在joda时间:我将更新我的答案以正确回答该问题。仅供参考,麻烦的旧日期时间类,例如,和
java.text.SimpleDateFormat
现在是,支持由内置于Java8和Java9中的类进行分类。请参阅。
String.format("On %s, a really cool thing happened.", DateTimeFormatter.print()DateTime.now());
package com.company;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        String example = "On 9/6/78, a really cool thing happened...";
        SimpleDateFormat replacement = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
        Pattern datePattern = Pattern.compile("\\b\\w{1,2}/\\w{1,2}/(?:\\w{4}|\\w{2})\\b");
        Date now = new Date();
        System.out.println(datePattern.matcher(example).replaceAll(replacement.format(now)));
    }
}