码迷,mamicode.com
首页 > 其他好文 > 详细

get current wee

时间:2018-02-06 14:56:09      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:tin   package   actual   str   util   time   ext   ring   --   

package com.sangame.uc.front.web.controller.front;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* Created by on 2017/4/17.
*/
public class DateUtils {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

public static void main(String args[]){
System.out.println(getCurrentMonday());
System.out.println(getPreviousSunday());
System.out.println(getMinMonthDate("2017-04-17"));
System.out.println(getMaxMonthDate("2017-04-17"));
}
// 获得本周一与当前日期相差的天数
public static int getMondayPlus() {
Calendar cd = Calendar.getInstance();
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) {
return -6;
} else {
return 2 - dayOfWeek;
}
}

// 获得当前周- 周一的日期
public static String getCurrentMonday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}


// 获得当前周- 周日 的日期
public static String getPreviousSunday() {
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
// 获得当前月--开始日期
public static String getMinMonthDate(String date) {
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}

// 获得当前月--结束日期
public static String getMaxMonthDate(String date){
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
}

get current wee

标签:tin   package   actual   str   util   time   ext   ring   --   

原文地址:https://www.cnblogs.com/watchfluture/p/8422279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!