How to render a String that is in this format yyyy-mm-dd HH:mm:ss.fff presentable

Asked

Viewed 271 times

1

I have a date that is a return from the database that comes in this format yyyy-mm-dd HH:mm:ss.fff, how can I turn this into dd-mm-yyyy HH:mm

Example: My date is like this: 2015-01-16 07:49:45.0 and I want to leave her that way: 16-01-2015 07:49

  • This solves your problem: http://answall.com/questions/25197/converter-data-dd-mm-aaaa-para-yyy-mm-ddthhmmss. Remembering that "M" (capital letters) should be used for the month.

  • I’m trying to use the String formatedDate = DateFormat.format("yyyy-MM-dd hh:mm:ss", receivedDate).toString(); but error in the .format

  • What error .format presents ?

  • the method format(date) in the type date format is not applicable for the arguments (String, Date)

2 answers

4


If you are going to treat only after you received from the bank need to do it here:

DateFormat dfBanco = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dfJava = new SimpleDateFormat("dd-MM-yyyy HH:mm");
String dataFormatada = dfJava.format(dfBanco.parse("2015-01-16 07:49:45.0"));

Exit:

16-01-2015 07:49
  • 1

    It worked perfectly, thank you very much.

1

In the code below, I’m using a Simpledataformat

This class will be responsible for storing the format and receiving the String objects to create the date in the desired format

public class MainTest {

    public static void main(String[] args) throws ParseException {
        java.util.Date suaData = new SimpleDateFormat("yyyy-MM-dd HH:mm")
                .parse("2015-01-16 07:49:45.0");//colocar origem da data
        System.out.println(suaData );
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.