Android 如何将字符串转换为日期格式 dd/MM/yyyy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21793347/
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
how to convert string to dateformat dd/MM/yyyy
提问by dragullar
String date = '15/2/2014'
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");
Date d = simpleDate.parse(date);
now Date d return : d= Sat Feb 15 00:00:00 MMT 2014
.
现在日期 d 返回:d= Sat Feb 15 00:00:00 MMT 2014
。
I want to get this format d= '15/2/2014'
我想得到这种格式 d= '15/2/2014'
How can I do it?
我该怎么做?
回答by Jay Vyas
Try this:
尝试这个:
String date="15/02/2014";
SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MM/yyyy");
try {
Date d=dateFormat.parse(date);
System.out.println("DATE"+d);
System.out.println("Formated"+dateFormat.format(d));
}
catch(Exception e) {
//java.text.ParseException: Unparseable date: Geting error
System.out.println("Excep"+e);
}
Hope this help.
希望这有帮助。
回答by Pratik Butani
You can try like this:
(Here you can change time variable
with your input string)
你可以这样试试:(在这里你可以改变time variable
你的输入字符串)
String time = "Sun Jul 15 2012 12:22:00 GMT+03:00 (FLE Daylight Time)";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
Date date = sdf.parse(time);
SimpleDateFormat sdf=new SimpleDateFormat("dd/M/yyyy");
String s=sdf.format(date.getTime());
回答by T8Z
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try
{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e)
{
e.printStackTrace();
}
回答by Dev 9
Try the code below
试试下面的代码
String date1 = "31/12/2011";
SimpleDateFormat form = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d1 = null;
Calendar tdy1;
try {
d1 = form.parse(date1);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tdy1 = Calendar.getInstance();
System.out.println("tdy mnthhhhhhhhhhh " + tdy1.get(Calendar.MONTH));
tdy1.setTime(d1);
System.out.println("Month of date1= " + tdy1.get(Calendar.MONTH));
Note
笔记
Calendar.MONTH is return integer value and it is start from 0 to 11 means 0=JANUARY and 11=DECEMBER
Calendar.MONTH 是返回整数值,从 0 到 11 表示 0=JANUARY 和 11=DECEMBER
for more detail check http://developer.android.com/reference/java/util/Calendar.html#JANUARY
有关更多详细信息,请查看http://developer.android.com/reference/java/util/Calendar.html#JANUARY
回答by Tariq Khalaf
Wouldn't you use just format before the ("dd/mm/yyyy")?
你不会在 ("dd/mm/yyyy") 之前使用格式吗?
String date = '15/2/2014'
SimpleDateFormat simpleDate = new Format("dd/MM/yyyy");
Date d = simpleDate.parse(date);