Here you can find the source of getCalendarByDateTime(int dateTime, String dateFormat)
public static Calendar getCalendarByDateTime(int dateTime, String dateFormat)
//package com.java2s; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static final String yyyyMMdd = "yyyyMMdd"; public static Calendar getCalendarByDateTime(int dateTime, String dateFormat) { Date date = toDate(dateTime, dateFormat); Calendar calendar = Calendar.getInstance(); calendar.setTime(date);/*from ww w . j a va 2s .c o m*/ return calendar; } public static Date toDate(int date, String timeFormat) { StringBuilder sbBuilder = new StringBuilder(); if (!yyyyMMdd.equals(timeFormat) && String.valueOf(date).length() == 8) { sbBuilder.append("0"); } sbBuilder.append(date); DateFormat sdf = new SimpleDateFormat(timeFormat); Date datetime = new Date(); try { datetime = sdf.parse(sbBuilder.toString()); } catch (ParseException e) { throw new RuntimeException("parse the time [" + date + "] with format [" + timeFormat + "] error", e); } return datetime; } public static Date toDate(int date, int time, String timeFormat) { StringBuilder sbBuilder = new StringBuilder(); sbBuilder.append(date); if (String.valueOf(time).length() == 8) { sbBuilder.append("0"); } sbBuilder.append(time); DateFormat sdf = new SimpleDateFormat(timeFormat); Date datetime = new Date(); try { datetime = sdf.parse(sbBuilder.toString()); } catch (ParseException e) { throw new RuntimeException( "parse the time [" + date + " " + time + "] with format [" + timeFormat + "] error", e); } return datetime; } }