0

I have this String which I want to convert in Date form like 2013-06-06

 "/Date(1370257470183+0530)/"

If anybody have Idea , How to do this programmatically please help me

Regards.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
Dinesh Chandra
  • 329
  • 1
  • 2
  • 27

4 Answers4

4
public static void main(String[] args) {
    String dateString = "/Date(1370257470183+0530)/";

    String longString = dateString.substring(6, dateString.indexOf('+'));
    String gmtString = dateString.substring(dateString.indexOf('+'), dateString.indexOf(')'));

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    format.setTimeZone(TimeZone.getTimeZone(gmtString));
    Date date = new Date(Long.parseLong(longString));
    System.out.println(format.format(date));

}
  • Extract number value by getting substring
  • Extract GMT string
  • Create a SimpleDateFormat
  • Format the timezone with the GMT string
  • Create a Date object from the first string
  • Pass the date to the formatter (return String)
  • Do something with that returned String
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
3

Here is what I coded. But before that you have to extract the timezone and the millisecond time so that you can create the Object. I think that you can do just doing some string operations upon the string.

Date date = new Date(1370257470183l); // here is your time
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+0530"));   // here is your timezone
String formattedDate = formatter.format(date);
System.out.println(formattedDate);
A Paul
  • 8,113
  • 3
  • 31
  • 61
0

You may use java.util.Date class and then use SimpleDateFormat to format the Date.

Date date=new Date(millis);

Reference: How to convert currentTimeMillis to a date in Java?

The input represents time in milliseconds from epoch ( Jan 1, 1970 GMT) by the way your input represents a date of 3rd June 2013

Community
  • 1
  • 1
user1933888
  • 2,897
  • 3
  • 27
  • 36
0

First you will have to search the String for the numeric values, link here: Search a String for Numerics

Then you want to turn the numeric value into a date. For that use SimpleDateFormat" link here:Oracle SimpleDateFormat

Community
  • 1
  • 1
nckbrz
  • 688
  • 1
  • 6
  • 20