2

i building a kml file and there is a line of timestamp:

<gx:TimeStamp>
          <when>2002-07-09T19:00:00-08:00</when>
        </gx:TimeStamp>

i need convert time like: "1430477311" to "2002-07-09T19:00:00-08:00" format

how ? (java code) tnx a lot

kfir91
  • 103
  • 1
  • 12

2 Answers2

2

You are wanting to convert from your timeformat to XML Date Format (ISO-8601) -

long timeStamp = 1430477311L;
java.util.Date yourDate = new java.util.Date(timeStamp*1000); //ms
SimpleDateFormat yyyyMMddTHHmmssSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
String date = yyyyMMddTHHmmssSDF.format(yourDate);

Simple Date Format reference.

kmort
  • 2,848
  • 2
  • 32
  • 54
farrellmr
  • 1,815
  • 2
  • 15
  • 26
  • Date Format (ISO-8601) is UTC time zone so must set time zone in the SimpleDateFormat instance to that. Otherwise you get the local time. – CodeMonkey May 14 '15 at 17:25
1

Simply pass the timestamp to a Date type:

Timestamp stamp = new Timestamp(inputTimestamp);
Date date = new Date(stamp.getTime());
//change the to the format that you need
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String timeStr = df.format(date);
E_net4
  • 27,810
  • 13
  • 101
  • 139