1

Server sending me time as 1390361405210+0530 so if I want to convert this in to date then should I have to add 0530 into 1390361405210 and then calculate date and time?

Any suggestion should be appreciated.Thanks

PPD
  • 5,660
  • 12
  • 52
  • 86
  • 3
    No, you don't want to add 530. You *might* want to add (or subtract) 5 hours and 30 seconds, but that's very different. Do you know *exactly* what date/time your sample is meant to represent? (In particular, you need to know whether it's in UTC or not...) Do you need to retain the UTC offset in your result? – Jon Skeet Nov 13 '14 at 10:59
  • Actually server sends only 1390361405210 but at time of json response 0530 is added automatically. Is that mean I dont have to add or subtract 5 hrs and 30 min? – PPD Nov 13 '14 at 11:05
  • 2
    It's really unclear what the context is. What is the server? What is creating the JSON? What are you trying to achieve? Please give more context, or it's very hard to help you. – Jon Skeet Nov 13 '14 at 11:30
  • @PPD `12131964` Is that the amount of deficit spending by the US federal government this minute, the price of a New York apartment, or my birthdate? You have to know the source of your data, and understand it's intended meaning. *We* here on StackOverflow don't know that; you must ask the source, as Jon Skeet suggests. Once you know, search StackOverflow for hundreds of Questions and Answers on converting strings or numbers into date-time objects. – Basil Bourque Nov 13 '14 at 18:10

3 Answers3

2

How about this.

    long currentDateTime = 1390361405210L;
    Date currentDate = new Date(currentDateTime);
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss Z");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+530"));
    System.out.println(sdf.format(currentDate));
Pankaj Shinde
  • 3,361
  • 2
  • 35
  • 44
1
public static void main( String[] args )
{
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    long milliSeconds=1390361405210L;
    Date date = new Date(milliSeconds);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    System.out.println(formatter.format(calendar.getTime())); 
    System.out.println(formatter.format(date)); 
}
Anand Pandey
  • 383
  • 4
  • 12
0

If we consider that the first part of the String is the number of milliseconds since the epoch, and the second part is a timezone indication (in that case, IST, Indian Standard Time), you can get a readable date like this :

final String jsonDate = "1390361405210+0530";
final Date date = new Date(Long.parseLong(jsonDate.substring(0, jsonDate.length() - 5)));
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT" + jsonDate.substring(jsonDate.length() - 5)));
System.out.println(format.format(date));

Output:

January 22, 2014 9:00:05 AM GMT+05:30

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47