Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { public static final int TYPE_DAY = 1; public static final int TYPE_WEEK = 2; public static final int TYPE_MONTH = 3; public static final int TYPE_YEAR = 4; public static long getPeriodStart(int periodType, long date) { final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(date); switch (periodType) { case TYPE_DAY: { cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); break; } case TYPE_WEEK: { cal.setFirstDayOfWeek(Calendar.MONDAY); final int currentDayOfWeek = (cal.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7; cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); break; } case TYPE_MONTH: { cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); break; } case TYPE_YEAR: { cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); break; } } return cal.getTimeInMillis(); } }