2011-10-03 87 views
-1

如何将日期格式“2011-09-07T00:00:00 + 02:00”更改为“dd.MM.”即“07.09”。如何在java中格式化日期?

在此先感谢!

+15

您是否搜索过“如何在java中设置日期格式?”在互联网上甚至在这个网站?我不这么认为。 –

回答

2

基本上 -

  1. 从上面的字符串创建一个日期格式对象

  2. 解析成日期对象,无论你喜欢格式化。

例如(我没有测试过这一点):

/* 
* REFERENCE: 
* http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 
*/ 
import java.text.DateFormat; 
import java.util.Date; 

public class DateFormatExample1 { 

    public static void main(String[] args) { 
     // Make a new Date object. It will be initialized to the current time. 
     DateFormat dfm = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); 
     Date d = dfm.parse("2011-09-07 00:00:00"); 

     // See what toString() returns 
     System.out.println(" 1. " + d.toString()); 

     // Next, try the default DateFormat 
     System.out.println(" 2. " + DateFormat.getInstance().format(d)); 

     // And the default time and date-time DateFormats 
     System.out.println(" 3. " + DateFormat.getTimeInstance().format(d)); 
     System.out.println(" 4. " + 
      DateFormat.getDateTimeInstance().format(d)); 

     // Next, try the short, medium and long variants of the 
     // default time format 
     System.out.println(" 5. " + 
      DateFormat.getTimeInstance(DateFormat.SHORT).format(d)); 
     System.out.println(" 6. " + 
      DateFormat.getTimeInstance(DateFormat.MEDIUM).format(d)); 
     System.out.println(" 7. " + 
      DateFormat.getTimeInstance(DateFormat.LONG).format(d)); 

     // For the default date-time format, the length of both the 
     // date and time elements can be specified. Here are some examples: 
     System.out.println(" 8. " + DateFormat.getDateTimeInstance(
      DateFormat.SHORT, DateFormat.SHORT).format(d)); 
     System.out.println(" 9. " + DateFormat.getDateTimeInstance(
      DateFormat.MEDIUM, DateFormat.SHORT).format(d)); 
     System.out.println("10. " + DateFormat.getDateTimeInstance(
      DateFormat.LONG, DateFormat.LONG).format(d)); 
    } 
} 
+0

你的第一个格式字符串不正确 - 月份是MM,而不是mm。 –

+0

...而你错过了'T'和日期/时间戳的时区部分。 –

+0

我将获得上述日期格式作为XML文件的输入,我需要将它转换为“dd.MM” – Beginner

6

这里是一个样本

编辑代码:

public static void main(String[] args) throws ParseException { 
    String input = "2011-09-07T00:00:00+02:00"; 
    SimpleDateFormat inputDf = new SimpleDateFormat("yyyy-MM-dd"); 
    SimpleDateFormat outputDf = new SimpleDateFormat("dd.MM"); 

    Date date = inputDf.parse(input.substring(0,9)); 
    System.out.println(date); 
    System.out.println(outputDf.format(date)); 
} 
+0

我将获得上述日期格式作为XML文件的输入,我需要将其转换到“dd.MM” – Beginner

+0

@neha检查您的要求的新代码。如果你不理解它,请尝试在eclipse中调试代码。你应该能够弄清楚 –

1

您的代码需要一点点的修正在以下行上

Date date = inputDf.parse(input.substring(0,9)); 

代替(0,9)您需要输入(0,10),您将获得所需的输出。

1

TL;博士

OffsetDateTime.parse("2011-09-07T00:00:00+02:00").format(DateTimeFormatter.ofPattern("dd.MM") 

java.time

问题等回答使用已被证明是麻烦和混乱的旧的遗留类。他们已经被java.time类取代。

您的输入字符串采用标准的ISO 8601格式。这些格式在java.time类中默认使用。所以不需要指定格式化模式。

OffsetDateTime odt = OffsetDateTime.parse("2011-09-07T00:00:00+02:00"); 

您可以生成你想要的格式的字符串。

DateTimeFormatter f = DateTimeFormatter.ofPattern("dd.MM"); 
String output = odt.format(f); 

MonthDay

你想月份和日期的日。实际上有一个课程,MonthDay

MonthDay md = MonthDay.from(odt); 

您可以生成所需格式的字符串。

DateTimeFormatter f = DateTimeFormatter.ofPattern("dd.MM"); 
String output = md.format(f);