Here you can find the source of toTimeStamp(Calendar cal)
public static String toTimeStamp(Calendar cal)
//package com.java2s; // See LICENSE.txt for license information import java.util.Calendar; public class Main { /**//from ww w . ja va 2 s . c om * Converts a Calendar object into a timestamp string in ISO 8601 format. */ public static String toTimeStamp(Calendar cal) { if (cal == null) { cal = Calendar.getInstance(); } StringBuilder sb = new StringBuilder(); sb.append(String.format("%04d", cal.get(Calendar.YEAR))); sb.append('-').append(String.format("%02d", cal.get(Calendar.MONTH) + 1)); sb.append('-').append(String.format("%02d", cal.get(Calendar.DAY_OF_MONTH))); sb.append('T').append(String.format("%02d", cal.get(Calendar.HOUR_OF_DAY))); sb.append(':').append(String.format("%02d", cal.get(Calendar.MINUTE))); sb.append(':').append(String.format("%02d", cal.get(Calendar.SECOND))); int ofs = cal.get(Calendar.ZONE_OFFSET); if (ofs != 0) { char sign = (ofs < 0) ? '-' : '+'; ofs = Math.abs(ofs); int ofsHour = ofs / 3600000; int ofsMin = (ofs / 60000) % 60; sb.append(sign).append(String.format("%02d", ofsHour)); sb.append(':').append(String.format("%02d", ofsMin)); } return sb.toString(); } }