183 lines
4.8 KiB
Plaintext
183 lines
4.8 KiB
Plaintext
|
package com.example.administrator.ximengjianyu.utils;
|
|||
|
|
|||
|
import java.text.SimpleDateFormat;
|
|||
|
import java.util.Calendar;
|
|||
|
import java.util.Date;
|
|||
|
|
|||
|
/**
|
|||
|
* Created by xukai on 2017/2/14.
|
|||
|
*/
|
|||
|
public class TimeUtils {
|
|||
|
|
|||
|
/**
|
|||
|
* 该方法用来获取当前的系统时间,并格式化事件
|
|||
|
*/
|
|||
|
public String getCurrentTime(){
|
|||
|
long timeMillis = System.currentTimeMillis();
|
|||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|||
|
Date curDate = new Date(timeMillis);
|
|||
|
String strData = formatter.format(curDate);
|
|||
|
return strData;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前年份
|
|||
|
*
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getYear() {
|
|||
|
return Calendar.getInstance().get(Calendar.YEAR);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前月份
|
|||
|
*
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getMonth() {
|
|||
|
return Calendar.getInstance().get(Calendar.MONTH) + 1;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前日期是该月的第几天
|
|||
|
*
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getCurrentDayOfMonth() {
|
|||
|
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前日期是该周的第几天
|
|||
|
*
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getCurrentDayOfWeek() {
|
|||
|
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前是几点
|
|||
|
*/
|
|||
|
public static int getHour() {
|
|||
|
return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//二十四小时制
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前是几分
|
|||
|
*
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getMinute() {
|
|||
|
return Calendar.getInstance().get(Calendar.MINUTE);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取当前秒
|
|||
|
*
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getSecond() {
|
|||
|
return Calendar.getInstance().get(Calendar.SECOND);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 根据传入的年份和月份,获取当前月份的日历分布
|
|||
|
*
|
|||
|
* @param year
|
|||
|
* @param month
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int[][] getDayOfMonthFormat(int year, int month) {
|
|||
|
Calendar calendar = Calendar.getInstance();
|
|||
|
calendar.set(year, month - 1, 1);//设置时间为每月的第一天
|
|||
|
//设置日历格式数组,6行7列
|
|||
|
int days[][] = new int[6][7];
|
|||
|
//设置该月的第一天是周几
|
|||
|
int daysOfFirstWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
|||
|
//设置本月有多少天
|
|||
|
int daysOfMonth = getDaysOfMonth(year, month);
|
|||
|
//设置上个月有多少天
|
|||
|
int daysOfLastMonth = getLastDaysOfMonth(year, month);
|
|||
|
int dayNum = 1;
|
|||
|
int nextDayNum = 1;
|
|||
|
//将日期格式填充数组
|
|||
|
for (int i = 0; i < days.length; i++) { //最大值为5,从0开始,长度为6
|
|||
|
for (int j = 0; j < days[i].length; j++) { //j的长度为7,最大值6
|
|||
|
if (i == 0 && j < daysOfFirstWeek - 1) { //第一行 并且小于该月第一天的周几-1
|
|||
|
// days[i][j] = 0;
|
|||
|
days[i][j] = daysOfLastMonth - daysOfFirstWeek + 2 + j;
|
|||
|
} else if (dayNum <= daysOfMonth) {
|
|||
|
days[i][j] = dayNum++;
|
|||
|
} else {
|
|||
|
days[i][j] = nextDayNum++;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return days;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 根据传入的年份和月份,判断上一个月有多少天
|
|||
|
*
|
|||
|
* @param year
|
|||
|
* @param month
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getLastDaysOfMonth(int year, int month) {
|
|||
|
int lastDaysOfMonth = 0;
|
|||
|
if (month == 1) {
|
|||
|
lastDaysOfMonth = getDaysOfMonth(year - 1, 12);
|
|||
|
} else {
|
|||
|
lastDaysOfMonth = getDaysOfMonth(year, month - 1);
|
|||
|
}
|
|||
|
return lastDaysOfMonth;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 根据传入的年份和月份,判断当前月有多少天
|
|||
|
*
|
|||
|
* @param year
|
|||
|
* @param month
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static int getDaysOfMonth(int year, int month) {
|
|||
|
switch (month) {
|
|||
|
case 1:
|
|||
|
case 3:
|
|||
|
case 5:
|
|||
|
case 7:
|
|||
|
case 8:
|
|||
|
case 10:
|
|||
|
case 12:
|
|||
|
return 31;
|
|||
|
case 2:
|
|||
|
if (isLeap(year)) {
|
|||
|
return 29;
|
|||
|
} else {
|
|||
|
return 28;
|
|||
|
}
|
|||
|
case 4:
|
|||
|
case 6:
|
|||
|
case 9:
|
|||
|
case 11:
|
|||
|
return 30;
|
|||
|
}
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 判断是否为闰年
|
|||
|
*
|
|||
|
* @param year
|
|||
|
* @return
|
|||
|
*/
|
|||
|
public static boolean isLeap(int year) {
|
|||
|
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
}
|