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

1

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

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

    
asked by anonymous 05.06.2015 / 19:18

2 answers

4

If you're going to deal with it only after it's received from the bank, you 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"));

Output:

16-01-2015 07:49
    
05.06.2015 / 19:40
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 );
    }
}
    
05.06.2015 / 19:40