0

Date is send in jsonserialised format i.e Date(1542002443000) from server.How can I convert to dd-mm-yyyy HH:mm:ss aa in android? I had already converted this type of format 2018-11-12T10:16:43 by this code.

Code:

public static String DateAndTimeCustom(String input) {
    String date = input.substring(0, input.indexOf("T"));
    String time = input.substring(input.indexOf("T") + 1, input.length());
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
    Date formattedDate = null;
    try {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        formattedDate = format.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return df.format(formattedDate) + " " + time;
}

How can i convert this Date(1542002443000)?

  • 1542002443000 seems to be a time-stamp, The date class has a constructor `Date(long date)` to allocate a Date object and initializes it with a time stamp – Guillaume Barré Nov 12 '18 at 08:13
  • how can i remove Date() function? – Abhishek Bardolia Nov 12 '18 at 08:19
  • Why do you want to remove it...? – Guillaume Barré Nov 12 '18 at 08:21
  • I mean i am getting in this format Date(1542002443000) which is coming as response \"appCreatedDate\":\"\\/Date(1542002443000)\\/\" – Abhishek Bardolia Nov 12 '18 at 08:27
  • Just added an answer cleaning `Date(1542002443000)` to get a clean time-stamp... – Guillaume Barré Nov 12 '18 at 08:38
  • response should be >>>\"appCreatedDate\":\"\\/1542002443000\\/\" . Please ask your backend developers to fix it otherwise it is very bad structure – Zaid Mirza Nov 12 '18 at 08:43
  • 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. Nov 12 '18 at 09:50

3 Answers3

1

As I mentioned in my comment because the value you receive is a time-stamp you just have to use the following Date constructor.

Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Edited: As you specified that you are receiving this as entry "Date(1542002443000)" you can use regular expression to clean it removing all non numeric characters.

And then your code should be:

public static void main(String[] args) {
    String d = DateAndTimeCustom("Date(1542002443000)");
    System.out.println(d);
}

public static String CleanDate(String input) {
    //  remove all non numeric characters
    return input.replaceAll("[^\\d]", "");
}

public static String DateAndTimeCustom(String input) {
    // Build a date based on the cleaned time-stamp
    Date d = new Date(Long.parseLong(CleanDate(input)));
    SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy HH:mm:ss aa", Locale.ENGLISH);
    String formattedDate  = df.format(d);

    return  formattedDate;

}
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
0

You can use this like following:-

String longDate = "1542002443000";
long mSecond = Long.parseLong(longDate);
String dString = new SimpleDateFormat("dd-mm-yyyy").format(new Date(mSecond));

If you are getting data in string format then you have to do type casting like above.

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
0

looks like the imestamp is saved in milliseconds maby try this ->

DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy hh:mm a");
     long dateStart = input;
                try {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(due);
                    ((TextView) view.findViewById(R.id.text)).setText(formatter.format(calendar.getTime()));

            } catch (Exception e) {
                e.printStackTrace();
            }