0

In PHP to convert a string to DateTime() its very very easy:

$dateTime = new DateTime("2013-12-11 10:109:08");
echo $dateTime->format("d/m/Y"); // output 11/12/2013

What is the equivalent in Java? I've seen a lot of questions in stackoverflow. I cant find a way to solve this problem.

My last try is:

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ITALIAN);
return dateFormat.format(new Date(datetime)).toString();

This crash application. Android Studio tells me that Date(java.lang.String) is deprecated.

Can someone help me?

4 Answers 4

2
// First convert the String to a Date
String dateTime = "2013-11-12 13:14:15";
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ITALIAN);
Date date = dateParser.parse(dateTime);
// Then convert the Date to a String, formatted as you dd/MM/yyyy
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormatter.format(date));

You can let the parser / formatter take the timezone into account by using SimpleDateFromat.setTimeZone() if you have to deal with TimeZones that are not in your default locale.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

 String time1="";
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US);
                             GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
                             calendar.setTimeInMillis(yourmilliseconds);
                                 time1=sdf.format(calendar.getTime());

Comments

0

Yes As of JDK version 1.1, Date(java.lang.String) is deprecated and replaced by DateFormat.parse(String s).

Comments

0

Parse it like that:

SimpleDateFormat formatter = 
            new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY);

        Calendar date = Calendar.getInstance();
        try {
            date.setTime(formatter.parse("12.12.2010"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

Have a look at my Android date picker example here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.