0

I have a string of date which is in the format of hh:mm. I want to convert it into yyyy-MM-dd HH:mm:ss this format and convert it into UTC time zone and save it into database.

I tried this:

  private void returnPickupTimeUTC(){
        String pickupTime = "04:05 pm";
        Date pickDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
        try{
           pickDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(pickupTime);
        } catch (ParseException ex){

        }
Calendar calendar = Calendar.getInstance();
        calendar.setTime(pickDate);
        Date time = calendar.getTime();
        SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US);
        outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
        pickupTimeStr =outputFmt.format(time);
        System.out.println(outputFmt.format(time));
    }
  • 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. – Ole V.V. May 08 '19 at 16:14
  • Does your code work? If not, precisely how does it misbehave? Please quote expected result, observed result and any error messages verbatim.So we may be able to help you. – Ole V.V. May 08 '19 at 16:16
  • 1
    Possible dulpicate of (1) [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) (2) [Convert String Date to String date different format [duplicate\]](https://stackoverflow.com/questions/14999506/convert-string-date-to-string-date-different-format) – Ole V.V. May 08 '19 at 16:21

2 Answers2

0

Since you only have hh:mm, to create yyyy-MM-dd HH:mm you need to give it yyyy-MM-dd.

try {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    pickDate = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US)
            .parse(year+"-"+month+"-"+day+" " + pickupTime);

} catch (ParseException e) {
    e.printStackTrace();
}
Israel dela Cruz
  • 794
  • 1
  • 5
  • 11
0

Here is my solution:

private void returnPickupTimeUTC() {
    try {
        String pickupTime = "04:05 pm";

        // Use this to get hour and minute from `pickupTime`
        SimpleDateFormat defaultSdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
        Calendar pickUpDate = Calendar.getInstance(Locale.getDefault());
        pickUpDate.setTime(defaultSdf.parse(pickupTime));

        // Set the hour and minute for current date.
        Calendar now = Calendar.getInstance(Locale.getDefault());
        now.set(Calendar.HOUR, pickUpDate.get(Calendar.HOUR));
        now.set(Calendar.MINUTE, pickUpDate.get(Calendar.MINUTE));

        // Convert `pickupTime` from "hh:mm a" to "yyyy-MM-dd HH:mm:ss" with UTC time zone.
        SimpleDateFormat utcSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        utcSdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        pickupTimeStr = utcSdf.format(now.getTime());
        System.out.println(pickupTimeStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Son Truong
  • 13,661
  • 5
  • 32
  • 58