-1

I want to convert Date format from 2017-02-08 00:00:00.0 to dd/MM/yyyy(08/02/2017). I tried with the following code.

          String dateInString  =bean.getDate();
          Date date = null;
          SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

          if (bean.getDate().matches("^[0-9]{2,4}(-[0-9]{1,2}){2}\\s[0-9]{1,2}(:[0-9]{1,2}){2}\\.[0-9]{1,}$")) {
                try {
                    date = formatter.parse(dateInString);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
          } 

But I am getting NullPointerException in date = formatter.parse(dateInString); line.

lin
  • 17,956
  • 4
  • 59
  • 83
Shiva Goud A
  • 322
  • 1
  • 6
  • 18

4 Answers4

2

You can use the below code:

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
String date = "2017-02-08 00:00:00.0";
try {
    Date dateNew = format1.parse(date);
    String formatedDate = format2.format(dateNew);
    System.out.println(formatedDate);
} catch (ParseException e) {
    e.printStackTrace();
}
Poornima
  • 174
  • 8
0

create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings. If you've tried SimpleDateFormat and it didn't work, then please show your code and any errors you may receive.

MM and mm both are different. MM points Month, mm points minutes
Some example for months

'M' - 7  (without prefix 0 if it is single digit)
'M' - 12
'MM' - 07 (with prefix 0 if it is single digit)
'MM' - 12
'MMM' - Jul (display with 3 character)
'MMMM' - December (display with full name)

Some example for minutes

'm' - 3  (without prefix 0 if it is single digit)
'm' - 19
'mm' - 03 (with prefix 0 if it is single digit)
'mm' - 19
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Shekhar Jadhav
  • 1,035
  • 9
  • 20
0
String dateInString = "2017-02-08 00:00:00.0";
Date date = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
    date = formatter.parse(dateInString);
} catch (ParseException e) {
      e.printStackTrace();
}
SimpleDateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter2.format(date));
naro
  • 416
  • 7
  • 16
0

The other Answers are outdated, using troublesome old date-time classes such as SimpleDateFormat.these old classs are now legacy, supplanted by the java.time classes.

LocalDateTime

Parse your input string as a LocalDateTime as the input lacks any indication of offset-from-UTC or time zone.

The input string nearly complies with standard ISO 8601 formatting. The standard formats are used by default with the java.time classes. Replace the SPACE in middle with a T.

String input = "2017-02-08 00:00:00.0".replace( " " , "T" );
LocalDateTime ltd = LocalDateTime.parse( input );

LocalDate

Extract a date-only object.

LocalDate ld = ltd.toLocalDate();

DateTimeFormatter

Generate a string representing that object's value. Specify your desired formatting pattern.

Locale l = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ).withLocale( l );
String output = ld.format( f );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154