how to convert string to dateformat dd/MM/yyyy

2020-06-17 04:19发布

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.

I want to get this format d= '15/2/2014'

How can I do it?

标签: android
5条回答
倾城 Initia
2楼-- · 2020-06-17 05:08

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

for more detail check http://developer.android.com/reference/java/util/Calendar.html#JANUARY

查看更多
倾城 Initia
3楼-- · 2020-06-17 05:12

You can try like this: (Here you can change time variable with your input string)

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());
查看更多
Melony?
4楼-- · 2020-06-17 05:13

Wouldn't you use just format before the ("dd/mm/yyyy")?

String date = '15/2/2014'
SimpleDateFormat simpleDate = new Format("dd/MM/yyyy");
Date d = simpleDate.parse(date);
查看更多
混吃等死
5楼-- · 2020-06-17 05:15

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.

查看更多
够拽才男人
6楼-- · 2020-06-17 05:20
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();
}
查看更多
登录 后发表回答