0

How can I format this DateTime form SQL 2019-02-18T06:30:38.9933333 to 2/18/19, 6:30 AM in the android studio using java

I already try Simpledatetime nothing happens.`

String startDate="2019-02-18T06:30:38.9933333"; // Input String
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy h:mm tt"); 

        java.util.Date date = sdf1.parse(startDate); 
        java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());

need help.. thannk you.

xingbin
  • 27,410
  • 9
  • 53
  • 103
Pao D.
  • 17
  • 3
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and the two `Date` classes, 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. – Ole V.V. Feb 22 '19 at 08:02
  • It’s hardly true that nothing happens. On my Java 10 I get `java.lang.IllegalArgumentException: Illegal pattern character 't'`. If you didn’t see it, it would seem that you are having a serious problem with your project setup. Please fix first thing, then report what you get so we know with what we should try to help you. – Ole V.V. Feb 22 '19 at 08:05
  • 1
    Don’t get a datetime as string from SQL. If your API level and JDBC driver don’t yet allow you to get a `LocalDateTime`, get a `Timestamp` and consider converting to `LocalDateTime` for formatting. – Ole V.V. Feb 22 '19 at 08:09
  • Possible duplicate of [Exceptions are not being caught in Android Studio when debugging](https://stackoverflow.com/questions/52963744/exceptions-are-not-being-caught-in-android-studio-when-debugging)(?) – Ole V.V. Feb 22 '19 at 08:11
  • 1
    That input string is not in SQL format, it is in standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. SQL format is similar, but without the `T` in the middle. – Basil Bourque Feb 22 '19 at 19:08

3 Answers3

1

Maybe you are use java.time.LocalDateTime if you are working with java8 or higher:

String startDate = "2019-02-18T06:30:38.9933333"; // Input String
LocalDateTime localDateTime = LocalDateTime.parse(startDate);
xingbin
  • 27,410
  • 9
  • 53
  • 103
1

java.time, ThreeTenABP and a built-in localized format

    DateTimeFormatter formatter
            = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                    .withLocale(Locale.US);

    String startDate = "2019-02-18T06:30:38.9933333"; // Input String
    LocalDateTime dateTime = LocalDateTime.parse(startDate);
    System.out.println(dateTime.format(formatter));

Output:

2/18/19 6:30 AM

Still better than the above, get a LocalDateTime from your database, or if this is not possible yet, then a Timestamp that you convert like this:

    LocalDateTime dateTime = DateTimeUtils.toLocalDateTime(timestampFromDatabase);

Or if using Java 8 or later:

    LocalDateTime dateTime = timestampFromDatabase.toLocalDateTime();

The formatting is as before.

As a rule Java knows the best format for a particular audience better than you do. Using the built-in format also saves you from the error-prone task of writing a format pattern string.

In the first snippet I am exploiting the fact that your input string conforms with ISO 8601, the international standard format that the java.time classes parse (and also print) as their default, that is, without any explicit formatter.

The classes SimpleDateFormat, java.util.Date and java.sql.Date are all poorly designed and long outdated. Instead I use java.time, the modern Java date and time API.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Use format:

yyyy-MM-dd'T'HH:mm:ss.SSSSSSS

For more Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

bhumik
  • 231
  • 2
  • 11