package com.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
*
* G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D
* 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时
* 在上午或下午 (0~11) z 时区
*
*/
public class DataUtils {
// 1.获取当前时间(YYYY-MM-dd HH:mm:ss)
public static String dangqianshijian() {
Date date = new Date();
SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = dFormat.format(date);
return dateString;
}
// 2.获取当前年月日时分秒
public static Map<String, String> ymdhmsw() {
Calendar calendar = Calendar.getInstance();
Date date = new Date();
calendar.setTime(date);
String year = calendar.get(Calendar.YEAR) + "年";
int m = calendar.get(Calendar.MONTH) + 1;
String month = "";
if (m < 10) {
month = "0" + m + "月";
} else {
month = m + "月";
}
int d = calendar.get(Calendar.DAY_OF_MONTH);
String day = "";
if (d < 10) {
day = "0" + d + "日";
} else {
day = d + "日";
}
String hour = calendar.get(Calendar.HOUR_OF_DAY) + "时";
String minute = calendar.get(Calendar.MINUTE) + "分";
String second = calendar.get(Calendar.SECOND) + "秒";
String yweek = "一年中第几周:" + calendar.get(Calendar.WEEK_OF_YEAR);
String mweek = "一月中第几周:" + calendar.get(Calendar.WEEK_OF_MONTH);
/*
* System.out.println(year + month + day + hour + minute + second);
* System.out.println(yweek); System.out.println(mweek);
*/
Map<String, String> map = new HashMap<String, String>();
map.put("year", year);
map.put("month", month);
map.put("day", day);
map.put("hour", hour);
map.put("minute", minute);
map.put("second", second);
map.put(yweek, yweek);
map.put("mweek", mweek);
return map;
}
// 3.时间差天数
public static String shijiancha() throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = df.parse("2018-11-12 12:12:12");
Date d2 = df.parse("2017-12-11 11:11:11");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
return days + "";
}
// 4.根据年月查询最后一天
public static int yueday(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
int day = calendar.getActualMaximum(Calendar.DATE);
return day;
}
public static void main(String[] args) throws Exception {
// 1.
/* System.out.println(dangqianshijian()); */
// 2.
/*Map<String, String> map=ymdhmsw();
System.out.println(map.get("year")+map.get("month"));*/
// 3.
/* System.out.println(shijiancha()); */
// 4.
int day = yueday(2018, 9);// 2018年10月
// 根据年月输出所有天数
for (int i = 1; i <= day; i++) {
System.out.print(i + " ");
}
}
}