Here you can find the source of toString(final Calendar calendar, final String format)
public static String toString(final Calendar calendar, final String format)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009 Lifeform Software. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*w ww .ja v a2 s . com*/ * Ernan Hughes - initial API and implementation *******************************************************************************/ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.StringTokenizer; import java.util.TimeZone; public class Main { public static String toString(final long time) { Calendar result = Calendar.getInstance(); result.setTimeInMillis(time); return toString(result); } public static String toString(final Calendar calendar) { return toString(calendar, "d MMM yyyy hh:mm aaa"); } public static String toString(final Calendar calendar, final String format) { final SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(calendar.getTime()); } public static Calendar getTime(final String time, final TimeZone tz) throws Exception { int hours, minutes; StringTokenizer st = new StringTokenizer(time, ":"); int tokens = st.countTokens(); if (tokens != 2) { String msg = "Time " + time + " does not conform to the HH:MM format."; throw new Exception(msg); } String hourToken = st.nextToken(); try { hours = Integer.parseInt(hourToken); } catch (NumberFormatException nfe) { String msg = hourToken + " in " + time + " can not be parsed as hours."; throw new Exception(msg); } String minuteToken = st.nextToken(); try { minutes = Integer.parseInt(minuteToken); } catch (NumberFormatException nfe) { String msg = minuteToken + " in " + time + " can not be parsed as minutes."; throw new Exception(msg); } if (hours < 0 || hours > 23) { String msg = "Specified hours: " + hours + ". Number of hours must be in the [0..23] range."; throw new Exception(msg); } if (minutes < 0 || minutes > 59) { String msg = "Specified minutes: " + minutes + ". Number of minutes must be in the [0..59] range."; throw new Exception(msg); } Calendar period = Calendar.getInstance(tz); period.set(Calendar.HOUR_OF_DAY, hours); period.set(Calendar.MINUTE, minutes); // set seconds explicitly, otherwise they will be carried from the // current time period.set(Calendar.SECOND, 0); return period; } }