在 Java 中从 MM/DD/YYYY 到 DD-MMM-YYYY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4169634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Going from MM/DD/YYYY to DD-MMM-YYYY in java
提问by javan_novice
Is there a method in Java that I can use to convert MM/DD/YYYY
to DD-MMM-YYYY
?
Java中是否有一种方法可以用来转换MM/DD/YYYY
为DD-MMM-YYYY
?
For example: 05/01/1999
to 01-MAY-99
例如:05/01/1999
到01-MAY-99
Thanks!
谢谢!
回答by Brian Clements
Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.
使用 SimpleDateFormat 解析日期,然后使用具有所需格式的 SimpleDateFormat 将其打印出来。
Here's some code:
这是一些代码:
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));
Output:
输出:
01-May-99
回答by zod
formatter = new SimpleDateFormat("dd-MMM-yy");
回答by CoolBeans
Below should work.
下面应该工作。
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date oldDate = df.parse(df.format(date)); //this date is your old date object
回答by ASIK RAJA A
Try this,
尝试这个,
Date currDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = dateFormat.format(currDate);
System.out.println("strCurrDate->"+strCurrDate);
回答by Jatin Devani
Try this
尝试这个
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();
回答by Md. Asaduzzaman
java.time
时间
You should use java.timeclasses with Java 8 and later. To use java.time, add:
您应该在 Java 8 及更高版本中使用java.time类。要使用java.time,请添加:
import java.time.* ;
Below is an example, how you can format date.
下面是一个示例,说明如何格式化日期。
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));
回答by Kanke
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println("Formatted Date: " + formatter.format(localDate));
Java 8 LocalDate
Java 8 本地日期