2

I'm stuck with converting DateTime format to String, I just can't get any idea how to get only Hours Minutes and Seconds from this type correctly. When I tried my way I get something like 2020-01-17T20:19:00. But I need to get just 20:19:00.

import org.joda.time.DateTime; 

public DateTime orderDateFrom;
Log.d(TAG, orderDateFrom.toString());
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

4 Answers4

2
This will get time as 23:10:04 format

import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;


        public DateTime orderDateFrom;

        Date d = orderDateFrom.toDate();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String formattedDate = sdf.format(d);
Dreamer
  • 517
  • 1
  • 4
  • 11
  • Thank you, that is exatly what I want! – Danila says Reinstate Monica Jan 17 '20 at 20:09
  • 1
    Happy I could help :) – Dreamer Jan 17 '20 at 20:13
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not when the questioner is already using the more modern and more programmer-friendly Joda-Time. Today we even have even better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 17 '20 at 21:00
2

Try this

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
System.out.println(formatter.format(date));
Vijay
  • 575
  • 4
  • 14
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome SimpleDateFormat class. At least not when the questioner is already using the more modern and more programmer-friendly Joda-Time. Today we even have even better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 18 '20 at 09:23
2

Since you are already using Joda-Time, I recommend you choose between two options:

  1. Stick with Joda-Time for now.
  2. Upgrade to java.time, the modern Java date and time API and the successor of Joda-Time.

The option I certainly whole-heartedly discourage is going back to Date and SimpleDateFOrmat from Java 1.0 and 1.1. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome.

Stick with Joda-Time

If you want the time from your DateTime formatted into a String, for example for output to the user:

    DateTime orderDateFrom = new DateTime(2020, 1, 17, 20, 19, 0, DateTimeZone.forID("Mexico/BajaSur"));

    DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("HH:mm:ss");
    String formattedTime = orderDateFrom.toString(timeFormatter);
    System.out.println("Order time: " + formattedTime);

Output from this snippet is:

Order time: 20:19:00

If you want the time of day as an object that you can use for further procesing:

    LocalTime orderTime = orderDateFrom.toLocalTime();
    System.out.println("Order time: " + orderTime);

Order time: 20:19:00.000

We notice that this time three decimals on the second of minute are also printed since the no-arg toString method does that. You can format the LocalTime using the same formatter as above to obtain the same string if you like.

A note on java.time on Android

If programming for Android API level 26 and/or above, java.time comes built-in. If you need to take lower API levels into account, java.time comes as an external dependency just like Joda-Time: the ThreeTenABP. That’s ThreeTen for JSR-310, where java.time was first described, and ABP for Android Backport. See the links at the bottom.

The code will be similar, not identical to the code using Joda-Time above.

Links

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

There is probably a better way to do this but you could just get the substring.

String orderDateStr = orderDateFrom.toString();
orderDateStr.substring(orderDateStr.lastIndexOf("T")+1);
rhowell
  • 1,165
  • 8
  • 21