0

I am working on an Android app.

I get a date String and a time string from a JSON file.

fecha_reporte = "2017-12-17" 

hora_reporte = "23:51:00"

I need to convert both strings into a date variable, then later I will need to make some calculations with it.

This is what I have so far:

String fecha = fecha_reporte + " " + hora_reporte;

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd H:m:s");
String dateInString = fecha;

try {

    Date date2 = formatter.parse(dateInString);
    System.out.println(date2);
    System.out.println(formatter.format(date2));
    Log.d("DURACION","DURACION REPORTE: calculado: "+date2);

} catch (ParseException e) {
    e.printStackTrace();
}

The output is a date, but with this format:

Sun Dec 17 23:51:00 GMT-07:00 2017

I need it with following format: 2017-12-17 23:51:00

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • Possible duplicate of [Android, How can I Convert String to Date?](https://stackoverflow.com/questions/8573250/android-how-can-i-convert-string-to-date) – Vismay Patil Dec 18 '17 at 08:10
  • 1
    refer this https://stackoverflow.com/a/40176777/1848157 – Radhey Dec 18 '17 at 08:12
  • 2
    you need to change format inside SimpleDateFormat. – Radhey Dec 18 '17 at 08:13
  • You can check [converting-json-date-value-into-format-day-month](https://stackoverflow.com/questions/47767029/converting-json-date-value-into-format-day-month/47767221#47767221) .And use in your code . – KeLiuyue Dec 18 '17 at 08:15
  • 1
    You are mistaken, the `Date` doesn’t *have* the format you quote. Its `toString` method produces that format, and that cannot be changed (only in a subclass of `Date`, and you don’t want that). To format your date into a string of the desired format, you need a formatter and its `format` method. The `SimpleDateFormat` you already have should work. – Ole V.V. Dec 18 '17 at 08:21
  • 1
    Even on Android, where the notoriously troublesome and otherwise outdated `SimpleDateFormat` class is what you have built-in, it’s worth considering throwing this class away and getting [JSR-310, the modern Java date and time API also known as `java.time`](https://docs.oracle.com/javase/tutorial/datetime/). You get it for Android in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Dec 18 '17 at 08:24
  • Similar questions have been asked over and over before. To find a good answer fast use your search engine. – Ole V.V. Dec 18 '17 at 08:29
  • As you have no doubt noticed already, your second `System.out.println()` gives almost your needed format: `2017-12-17 23:51:0`. You probably want to use `mm` and `ss` in the format pattern string (parsing should still work). If you have any issue beyond this, you need to explain what it is. – Ole V.V. Dec 18 '17 at 08:35
  • Possible duplicate of [How do you format date and time in Android?](https://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – Ole V.V. Dec 18 '17 at 08:36
  • 1
    @OleV.V., you are right. I only needed to take the value from formatter.format(date2) – mvasco Dec 18 '17 at 08:46
  • In particular when you are going to do some calculations with the resulting date-time, give JSR-310 a shot, it is much better suited for date-time math. – Ole V.V. Dec 18 '17 at 11:52

1 Answers1

2

java.time

You are using troublesome old date time classes that are now legacy. Avoid them. Now supplanted by the java.time classes.

Parse your input strings as LocalDateTime as they lack information about time zone or offset-from-UTC.

Add a T to comply with standard ISO 8601 format.

String input = "2017-12-17" + "T" + "23:51:00" ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;

Generate a String in your desired format by calling toString and replace the T in the middle with a SPACE.

ldt.toString().replace( "T" , " " ) ;

Alternatively, generate strings in custom formats using DateTimeFormatter class.

For earlier Android, see the ThreeTen-Backport and ThreeTenABP projects.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154