在MM中从MM / DD / YYYY到DD-MMM-YYYY

时间:2010-11-12 22:27:48

标签: java date-format

我是否可以使用Java方法将MM/DD/YYYY转换为DD-MMM-YYYY

例如:05/01/199901-MAY-99

谢谢!

7 个答案:

答案 0 :(得分:22)

使用SimpleDateFormat解析日期,然后使用所需格式的SimpleDateFormat打印出来。

以下是一些代码:

    SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
    Date date = format1.parse("05/01/1999");
    System.out.println(format2.format(date));

输出:

01-May-99

答案 1 :(得分:2)

java.time

您应该在Java 8和更高版本中使用 java.time 类。要使用 java.time ,请添加:

import java.time.* ;

下面是一个示例,说明如何设置日期格式。

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String date = "15-Oct-2018";
LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate); 
System.out.println(formatter.format(localDate));

答案 2 :(得分:1)

试试这个,

Date currDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = dateFormat.format(currDate);
System.out.println("strCurrDate->"+strCurrDate);

答案 3 :(得分:1)

试试这个

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // Set your date format
        String currentData = sdf.format(new Date());
        Toast.makeText(getApplicationContext(), ""+currentData,Toast.LENGTH_SHORT ).show();

答案 4 :(得分:0)

formatter = new SimpleDateFormat("dd-MMM-yy");

答案 5 :(得分:0)

下面应该有效。

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date oldDate = df.parse(df.format(date)); //this date is your old date object

答案 6 :(得分:0)

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println("Formatted Date: " + formatter.format(localDate));

Java 8 LocalDate

相关问题