0

Bigger picture is I am writing a junit test that uses h2 database. I need to compare the values returned by the endpoint and do assertequals. The values in the response body is linked hash map. Keys are all strings. Values could be date or integer or strings or null.

I have stored the expected values in a string array. I loop through the values in the linked hash map and check if it is null or integer or date and then handle appropriately. Date value 2020-02-03 got converted to Long 1580709600000. I believe date somehow converted to timestamp. I am not able to convert it back to date . or the expected value back to timestamp. I am getting exception in both cases. How to fix this? Thanks for your time.enter image description here

    String values = "205,2020-02-03,Commodi";

        ResponseEntity<String> response = testRestTemplate.getForEntity(ResourceUrl, String.class);
        @SuppressWarnings("unchecked")
        List<Map<String, String>> list = objectMapper.readValue(response.getBody(), List.class);

        Iterator<Map.Entry<String, String>> it = list.get(0).entrySet().iterator();

        String expected_values[] = values.split(",", -1);
        int i = 0;

        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();

            if (expected_values[i].isEmpty())
                assertEquals(String.valueOf("null"), String.valueOf(entry.getValue()));
            else if (isInteger(expected_values[i],10))
            {
                assertEquals(Integer.valueOf(expected_values[i]), entry.getValue());
            }
            else if(isValidDate(expected_values[i]))
            {
                //long timestamp = Long.valueOf(expected_values[i].trim());
                //Long timestamp = Long.valueOf(entry.getValue().trim());
//                Timestamp ts=new Timestamp(entry.getValue());  
//                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
                Date date = new Date(Long.valueOf(entry.getValue().trim()));
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String formatedDate = format.format(date);
//                assertEquals(formatter.format(ts), entry.getValue());
                //Date date = Date.from(Instant.ofEpochMilli(Long.valueOf(entry.getValue().trim())));

            }
            else
            {
                assertEquals(expected_values[i], entry.getValue());
            }
            i++;
        }
Namagiri Sridhar
  • 177
  • 1
  • 6
  • 18

2 Answers2

2

You can input the long value onto an object date as a long, then you can use SimpleDateFormat to apply the pattern you wish see code below:

Date date = new Date(Long.valueOf(entry.getValue().trim()));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = format.format(date);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

Look at this: https://www.unixtimestamp.com/index.php

Your value 1580709600000 seems to be a timestamp in milliseconds. And if you take this part 1580709600 (seconds) and convert it over the link above to readable date-time you will get 02/03/2020 @ 6:00am (UTC).

this timestamp can be converted to Date:

Date date = Date.from(Instant.ofEpochMilli(1580709600000L));
  • Seems perfect. I tried this Date date = Date.from(Instant.ofEpochMilli(Long.valueOf(entry.getValue().trim()))); I get java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String – Namagiri Sridhar Mar 14 '20 at 01:53
  • 1
    Are you sure that you get Exception from this line? Because in this line you are converting String to Long and not the other way around. The Exception given above must be thrown somewhere else. Look for the line number in the stacktrace there you'll find line number where Exception is thrown. – Sergej Masljukow Mar 14 '20 at 09:45