Created
March 11, 2013 09:51
-
-
Save xiangzhuyuan/5133140 to your computer and use it in GitHub Desktop.
about date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class DemoDateUtil { | |
//static DateUtil du = new DateUtil(); | |
/** | |
* @param args | |
*/ | |
@SuppressWarnings("deprecation") | |
public static void main(String[] args) { | |
Calendar cal=Calendar.getInstance(); | |
Date date=cal.getTime(); | |
Date du1=getStartOfLastWeek(date); | |
Date du2=getStartOfWeek(date); | |
Date du3=getStartOfMonth(date); | |
Date du4=getStartOfLastMonth(date); | |
Date du5=getStartOfNextMonth(date); | |
System.out.println(du1.toLocaleString()); | |
System.out.println(du2.toLocaleString()); | |
System.out.println(du3.toLocaleString()); | |
System.out.println(du4.toLocaleString()); | |
System.out.println(du5.toLocaleString()); | |
/** | |
* 2013-3-4 17:49:44 | |
* 2013-3-11 17:49:44 | |
* 2013-3-1 17:49:44 | |
* 2013-2-1 17:49:44 | |
* 2013-4-1 17:49:44 | |
*/ | |
} | |
/** | |
* Start of the month | |
* | |
* @param inDate | |
* @return | |
*/ | |
public static Date getStartOfMonth(Date inDate) { | |
if (inDate == null) | |
return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(inDate); | |
calendar.set(Calendar.DAY_OF_MONTH, 1); | |
return calendar.getTime(); | |
} | |
/** | |
* Start of next month | |
* | |
* @param inDate | |
* @return | |
*/ | |
public static Date getStartOfNextMonth(Date inDate) { | |
if (inDate == null) | |
return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(inDate); | |
calendar.add(Calendar.MONTH, 1); | |
calendar.set(Calendar.DAY_OF_MONTH, 1); | |
return calendar.getTime(); | |
} | |
/** | |
* Start of last month | |
* | |
* @param inDate | |
* @return | |
*/ | |
public static Date getStartOfLastMonth(Date inDate) { | |
if (inDate == null) | |
return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(inDate); | |
calendar.add(Calendar.MONTH, -1); | |
calendar.set(Calendar.DAY_OF_MONTH, 1); | |
return calendar.getTime(); | |
} | |
/** | |
* Start of last week | |
* | |
* @param inDate | |
* @return | |
*/ | |
public static Date getStartOfLastWeek(Date inDate) { | |
if (inDate == null) | |
return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(inDate); | |
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { | |
calendar.add(Calendar.WEEK_OF_MONTH, -1); | |
} | |
calendar.add(Calendar.WEEK_OF_MONTH, -1); | |
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); | |
return calendar.getTime(); | |
} | |
/** | |
* Start of the week | |
* | |
* @param inDate | |
* @return | |
*/ | |
public static Date getStartOfWeek(Date inDate) { | |
if (inDate == null) | |
return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(inDate); | |
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { | |
calendar.add(Calendar.WEEK_OF_MONTH, -1); | |
} | |
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); | |
return calendar.getTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment