-2

I am trying to convert this String into date/time format
I am getting this string from the database and just want to convert it into proper date and time...
String str = 2019-02-22T13:43:00Z;//input
I am getting proper date from this string by this code:

 String[] split_date = date_time.split("T",2);
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 Date   date1  = format.parse ( split_date[0]);
 date_time = new SimpleDateFormat("dd-MMM-yyyy hh:mm", Locale.ENGLISH).format(date1);  

In above code (date_time = 22-Feb-2019 12:00) is coming.
expected output is : 22-feb-2019 13:43
The problem here is I can't get the proper time from that default format.

black-hacker
  • 223
  • 4
  • 12
  • 1
    What do you mean by proper date ? – Santanu Sur Mar 15 '19 at 05:57
  • if you want only this "22-Feb-2019" ? – Hitesh Tarbundiya Mar 15 '19 at 05:58
  • why are you splitting on `T`? – Scary Wombat Mar 15 '19 at 05:59
  • 1
    I'd look at parsing by using `Instant.parse(date_time)`, and then format to the timezone you need. You can then format to the desired output with any timezone needed. Don't use the old java `Date` classes. – KevinO Mar 15 '19 at 06:00
  • 1
    because of you split `T` take the only first part of it `2019-02-22` and formate with `dd-MMM-yyyy hh:mm` you add `hh:mm`. I don't see anywhere in your code you give `hh:mm`. and avoid Date class that is outdated use LocalDate class. – Akash Shah Mar 15 '19 at 06:12
  • @SantanuSur Proper time* – black-hacker Mar 15 '19 at 06:14
  • @HiteshTarbundiya i also want time in that, also edited – black-hacker Mar 15 '19 at 06:17
  • @ScaryWombat because there is T between them and atleast i am getting the date by that so... – black-hacker Mar 15 '19 at 06:18
  • @AkashShah if i give hh:mm the exception is coming – black-hacker Mar 15 '19 at 06:18
  • give your simple input and expected output. – Akash Shah Mar 15 '19 at 06:20
  • @AkashShah done – black-hacker Mar 15 '19 at 06:37
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. More details in [the answer by Basil Bourque](https://stackoverflow.com/a/55177418/5772882). – Ole V.V. Mar 15 '19 at 07:36
  • Near-duplicate of [ISO 8601 String to Date/Time object in Android](https://stackoverflow.com/questions/3941357/iso-8601-string-to-date-time-object-in-android) and [Android Studio Convert ISO string to “America/New_York” when adding to event to calendar](https://stackoverflow.com/questions/52670961/android-studio-convert-iso-string-to-america-new-york-when-adding-to-event-to) (since `2019-02-22T13:43:00Z` is an ISO 8601 formatted string) – Ole V.V. Mar 15 '19 at 07:39
  • As @AkashShah correctly states, you are throwing away the `13:43:00` part of the original string, which is why this information is not in your resulting string. – Ole V.V. Mar 15 '19 at 07:50

2 Answers2

2

The modern approach uses the java.time classes.

Instant instant = Instant.parse( "2019-02-22T13:43:00Z" ) ;
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu HH:mm" ) ;
String output = odt.format( f ) ;

output :

22-Feb-2019 13:43

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Call requires API level 26 (current min is 19): java.time.Instant#parse – Sagar Zala Mar 15 '19 at 08:05
  • 2
    Not really, @SagarZala. java.time has been backported. Add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your project and use the code in this answer. Just make sure to import from `org.threeten.bp`. – Ole V.V. Mar 15 '19 at 08:39
  • 1
    @SagarZala For Java 6 and 7, see the *ThreeTen-Backport* library. For earlier Android, see the *ThreeTenABP* library. – Basil Bourque Mar 15 '19 at 16:41
  • Sure. I will see that. Thanks. – Sagar Zala Mar 16 '19 at 05:13
0

Try below code:

String dt = "2019-02-22T13:43:00Z";
SimpleDateFormat mainformat = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", Locale.getDefault());
mainformat.setTimeZone(TimeZone.getTimeZone("UTC"));

try {
    Date date1 = mainformat.parse(dt);
    String date_time = new SimpleDateFormat("dd-MMM-yyyy HH:mm", Locale.ENGLISH).format(date1);
    Log.e("Date Time", date_time); 
} catch (Exception e) {
    e.printStackTrace();
}

Output: 22-Feb-2019 13:43

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • 1
    These terrible classes were supplanted years ago by the *java.time* classes. – Basil Bourque Mar 15 '19 at 07:08
  • @BasilBourque, Call requires API level 26 (current min is 19): java.time.Instant#parse – Sagar Zala Mar 15 '19 at 08:05
  • 1
    The question states: “expected output is : 22-feb-2019 **13**:43” (my boldfacing). And your parsing is incorrect. `Z` is a UTC offset and must be parsed as such, or you get an incorrect time. – Ole V.V. Mar 15 '19 at 08:45
  • 1
    capital ``HH`` gives 13 instead of 1. – Akash Shah Mar 15 '19 at 08:49
  • @AkashShah yes. – Sagar Zala Mar 15 '19 at 09:00
  • @OleV.V. says it give you an incorrect date when you print `date1` output is in my case `Fri Feb 22 13:43:00 IST 2019` because I am from India `IST`(but time is same) and date is `UTC`. you have to add one more line `mainformat.setTimeZone(TimeZone.getTimeZone("UTC"));` – Akash Shah Mar 15 '19 at 09:03
  • 1
    @SagarZala For Java 6 and 7, see the *ThreeTen-Backport* library. For earlier Android, see the *ThreeTenABP* library. – Basil Bourque Mar 15 '19 at 16:39