0

I am trying to get the start date in JSP page through Ajax call but its returning as Json object i.e 153452636268, I want to convert my Date from JSon to Java, can you please assist.

$.ajax({
        type: "GET",
        url: "${pageContext.request.contextPath}" + "/status/id?city="+city,
        async: false,
        cache : false,
        contentType: "application/json",
        success: function (response) {
        var $tab = $("#Table");

        $tab.css("display", "inline");             
        $.each(response.statusId, function(index,statusDetail) {
                    $tab.append("<tr><td align='center'><input id='Radio"+index+"' name='statRadio' title='"+index+"' type='radio'/></td>"+
                                "<td id='productId"+index+"' align='center' style='display: none;'>"+statusDetail.key.StudId+"</td>"+                                   
                                "<td id='startDate"+index+"' align='center'>"+statusDetail.key.startDate+"</td></tr>");
                   // 
                });                
        },
        failure: function (response) {
            return; 
        },
        error: function (response) {
            return;
        }
    });
Taplar
  • 24,246
  • 4
  • 18
  • 33
shwetha
  • 11
  • 5
  • What do you mean convert it to java? You mean javascript? Also is that number a timestamp in milliseconds, or what? – Taplar Jul 26 '18 at 21:00
  • I need to convert the date to java, currently its displaying in JSON, the correct date I should be fetching is 2018-07-23 00:00:00.0, instead of this its giving me 1532318400000 – shwetha Jul 26 '18 at 21:08
  • 1
    Your not making sense man. "convert to java" doesn't make sense in this context. Your ajax request is returning json, and you are wanting to turn that number into something, but it's not java. – Taplar Jul 26 '18 at 21:09
  • how do i do that? – shwetha Jul 26 '18 at 21:14

3 Answers3

0

If you have a variable with milliseconds, you can give it to the Date constructor to create a Date object, which you can use and manipulate however you want.

var milliseconds = 1532318400000;
                               //whatever variable holds your timestamp
var turnItIntoADate = new Date(milliseconds);

console.log(turnItIntoADate.toString());

I want to emphasize though that none of this is Java. This is all Javascript.

If you need to format the date in a particular fashion, you can see the associated question: How to format a JavaScript date

Taplar
  • 24,246
  • 4
  • 18
  • 33
0
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author Sumesh
 *
 */
public class MillisToDate {

    public static void main(String args[]) {

       //Converting milliseconds to Date using java.util.Date
       //current time in milliseconds
       long currentDateTime = Long.parseLong("153452636268");

       //creating Date from millisecond
       Date currentDate = new Date(currentDateTime);

       //printing value of Date
       System.out.println("current Date: " + currentDate);

       DateFormat df = new SimpleDateFormat("dd:MM:yy:HH:mm:ss");

       //formatted value of current Date
       System.out.println("Milliseconds to Date: " + df.format(currentDate));

       //Converting milliseconds to Date using Calendar
       Calendar cal = Calendar.getInstance();
       cal.setTimeInMillis(currentDateTime);
       System.out.println("Milliseconds to Date using Calendar:"
               + df.format(cal.getTime()));

       //copying one Date's value into another Date in Java
       Date now = new Date();

       System.out.println("original Date: " + df.format(now));
    }

}

Hope this will help you.

Sumesh TG
  • 2,367
  • 1
  • 12
  • 29
0
In JSP page. You can use JSP Tag library. 
 <jsp:useBean id="dateObject" class="java.util.Date" /> <jsp:setProperty name="dateObject" property="time" value="${timeInMilliSeconds}" /> 

Date is : 
<b><fmt:formatDate value="${dateObject }" pattern="dd/MM/yyyy"><b> 
Time is : 
<b><fmt:formatDate value="${dateObject }" pattern="hh:mm a" /></b>

    output:

    Date is : 26/02/2018 Time is : 06:27 PM
Harshit
  • 19
  • 1