Java
Chilkat2 Java
How to Parse a TimeStamp (such as 2016-11-11T14:32:17.0908971Z)
Timestamps are frequently used in REST API responses. This example demonstrates how to parse a timestamp string to get at the date/time components in the local timezone or in the GMT/UTC timezone.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.DtObj;
public class ChilkatExample {
public static void main(String[] args) {
// Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
String strTimestamp = "2016-11-11T14:32:17.0908971Z";
DateTime dateTime = new DateTime();
dateTime.setFromTimestamp(strTimestamp);
// Get a DtObj in the local timezone.
boolean bLocalTimezone = true;
DtObj dt = new DtObj();
dateTime.toDtObj(bLocalTimezone, dt);
// Get the individual date/time components
System.out.println("-- Local Time --");
System.out.println("Year: " + dt.getYear());
System.out.println("Month: " + dt.getMonth());
System.out.println("Day: " + dt.getDay());
System.out.println("Hour: " + dt.getHour());
System.out.println("Minutes: " + dt.getMinute());
System.out.println("Seconds: " + dt.getSecond());
// Get a DtObj in the GMT/UTC timezone.
bLocalTimezone = false;
dateTime.toDtObj(bLocalTimezone, dt);
// Get the individual date/time components
System.out.println("-- UTC Time --");
System.out.println("Year: " + dt.getYear());
System.out.println("Month: " + dt.getMonth());
System.out.println("Day: " + dt.getDay());
System.out.println("Hour: " + dt.getHour());
System.out.println("Minutes: " + dt.getMinute());
System.out.println("Seconds: " + dt.getSecond());
}
}