0
  • 2022-12-08T06:07:34.780Z

  • 12-10-2022 only

     Utility.log_d("Date12343",orderlist.getCreatedAt());
     /*Calendar c = Calendar.getInstance();
     SimpleDateFormat sdf = new SimpleDateFormat(orderlist.getCreatedAt(), Locale.ENGLISH);
     Log.e("TAG", "formatted string: "+sdf.format(c.getTime()));*/
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
     sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
     try {
         sdf.parse(orderlist.getCreatedAt());
     } catch (ParseException e) {
         e.printStackTrace();
     }
    
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). Also, ['Z' is not the same as Z](https://stackoverflow.com/a/67953075/10819573). – Arvind Kumar Avinash Dec 14 '22 at 09:38
  • 1
    Solution using java.time API (the modern date-time API): `OffsetDateTime.parse("2019-01-27T09:27:37Z").format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))`. – Arvind Kumar Avinash Dec 14 '22 at 09:38
  • Your input string, `2022-12-08T06:07:34.780Z`, is in UTC (denoted by the trailing `Z`). Am I assuming correctly that your user wants output in his or her own time zone? It is never the same date in all time zones. At the mentioned point in time it is still December 7 in California, for example. – Ole V.V. Dec 14 '22 at 12:37

1 Answers1

0

you have to format using simple date format.

String initialStringDate = "2019-01-27T09:27:37Z";
    Locale us = new Locale("US");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", us);
    try {
        Date date = format.parse(initialStringDate);
        String stringDate = new SimpleDateFormat("dd/MM/yyyy", us).format(date);
      

        Log.i("Date", "" + stringDate );
    } catch (ParseException e) {
        e.printStackTrace();
    }
Dhruv Sakariya
  • 792
  • 4
  • 10
  • here stringDate is your formated date. – Dhruv Sakariya Dec 14 '22 at 06:10
  • 1
    W/System.err: java.text.ParseException: Unparseable date: "2022-12-08T07:46:40.043Z" W/System.err: at java.text.DateFormat.parse(DateFormat.java:362) – Vishal Rastogi Dec 14 '22 at 07:20
  • String StartDate = binding.startDate.getText().toString(); Log.d("Date1",StartDate); // 08/12/2022 String date= StartDate; SimpleDateFormat spf=new SimpleDateFormat("dd-MM-yyyy"); String newDate= spf.format(date); spf= new SimpleDateFormat("yyyy-MM-dd"); String date1 = spf.format(newDate); Log.d("Date2",date1); – Vishal Rastogi Dec 14 '22 at 08:01
  • 1
    Above code what is the problem – Vishal Rastogi Dec 14 '22 at 08:01
  • 1
    The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). Also, ['Z' is not the same as Z](https://stackoverflow.com/a/67953075/10819573). – Arvind Kumar Avinash Dec 14 '22 at 09:34
  • 1
    Solution using java.time API (the modern date-time API): `OffsetDateTime.parse("2019-01-27T09:27:37Z").format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))` – Arvind Kumar Avinash Dec 14 '22 at 09:37
  • 1
    (1) In my most honest opinion helping people how to use `SimpleDateFormat` is doing them an ill favour. No one should use that notoriously troublesome class at all in 2022. (2) The question asked about `2022-12-08T06:07:34.780Z` with three decimals of fraction of second, and your code cannot handle that. You are answering a different question. – Ole V.V. Dec 14 '22 at 12:43