Java-格式化日期打印

Java-格式化日期打印,java,date,Java,Date,我需要帮助在下面的代码格式的日期。应为表格上显示的日期:05。2014年5月。请给我建议怎么做 package person; public class Person { public static void main(String[] split) { String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow"; S

我需要帮助在下面的代码格式的日期。应为表格上显示的日期:
05。2014年5月
。请给我建议怎么做

package person;
public class Person {

public static void main(String[] split) {

    String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow";
    String[] newText = text.split("[./ ]");
    for(int i=0; i<newText.length; i+=4)
    {
         String name = newText[i].split(" ")[0];
         String lastName = newText[i+1].split(" ")[0];
         String dateOfBirth = newText[i+2].split(" ")[0];
         String placeOfBirth = newText[i+3].split(" ")[0];

         System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + dateOfBirth + ", place of birth: " + placeOfBirth);
    }
}

}
包人;
公共阶层人士{
公共静态void main(字符串[]拆分){
字符串text=“John.Davidson/05051988/贝尔格莱德Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow”;
字符串[]newText=text.split(“[./]”);
对于(inti=0;i请尝试这种方法

String dateOfBirth = newText[i+2].split(" ")[0];
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.ENGLISH);
Date date = dateFormat.parse(dateOfBirth);
SimpleDateFormat dF = new SimpleDateFormat("dd. MMMM yyyy", Locale.ENGLISH);
System.out.println(dF.format(date));
不要忘记处理异常

import java.text.ParseException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Format {


    public static void main(String[] args) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("dd ',' MMM yyyy", Locale.ENGLISH);
        SimpleDateFormat parser = new SimpleDateFormat("ddMMyyyy");
        String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow";
        String[] newText = text.split("[./ ]");
        for(int i=0; i<newText.length; i+=4)
        {
            String name = newText[i].split(" ")[0];
            String lastName = newText[i+1].split(" ")[0];
            String dateOfBirth = newText[i+2].split(" ")[0];
            String placeOfBirth = newText[i+3].split(" ")[0];


            System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + formatter.format(parser.parse(dateOfBirth)) + ", place of birth: " + placeOfBirth);
        }}


}
导入java.text.simpleDataFormat; 导入java.util.Locale; 公共类格式{ 公共静态void main(字符串[]args)引发异常{ SimpleDataFormat格式化程序=新的SimpleDataFormat(“dd”,“MMM-yyy”,Locale.ENGLISH); SimpleDataFormat解析器=新的SimpleDataFormat(“ddmmyyy”); 字符串text=“John.Davidson/05051988/贝尔格莱德Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow”; 字符串[]newText=text.split(“[./]”);
对于(inti=0;i建议:使用SDK中的实用程序类:

  • Java 8:
  • 在Java8之前:尤其是

    • 你可以这样做

      请记住,不是线程安全的

      日期格式未同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一种格式,则必须在外部同步


      您使用的是哪种Java版本?您确定这有效吗?我一直认为
      m
      是为minutes@SparkOntx编码为fast。已更新
          SimpleDateFormat FromDate = new SimpleDateFormat("mmDDyyyy");
          Date date = FromDate.parse("05051988");
          SimpleDateFormat toFormat = new SimpleDateFormat("dd. MMMM yyyy");
          String result = toFormat.format(date);