DateUtils.java
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.xdy.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* @author 蒋洪波
* @version 版本号
* @file DateUtils.java
* @brief 时间工具类
* 详细说明,如果没有请删除
* @date 2017/10/12
* Copyright (c) 2017, 学点云
* All rights reserved.
*/
public class DateUtils {
/**
* 获得到分的时间
* @brief 获得月份时间
*/
public static String getTimeWithMin() {
return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(new Date());
}
/**
* 获得到分的时间
* @brief 获得月份时间
*/
public static String getTimeWithDay() {
return new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date());
}
/**
* 获得到秒的时间
* @return yyyy-MM-dd HH:mm:ss 完整格式的时间
*/
public static String getTimeWithSec() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date());
}
/**
* 获得到毫秒的时间
* @return yyyy-MM-dd HH:mm:ss.SSS 带毫秒的完整格式的时间
*/
public static String getTimeWithMs() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINA).format(new Date());
}
/**
* @return yyyy-MM 年月
* @brief 获取年月
*/
public static String getYearMonth() {
int year, month;
Calendar calendar = Calendar.getInstance();
/**
* 获取年份
*/
year = calendar.get(Calendar.YEAR);
/**
* 获取月份
*/
month = calendar.get(Calendar.MONTH) + 1;
String data = year + "-" + (month < 10 ? "0" + month : month);
return data;
}
/**
* @param data
* @return
* @brief 毫秒值
*/
public static Date stringToDate(String data) {
Date parse = null;
try {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
parse = f.parse(data);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
public static final String getData(Date mData, String format) {
SimpleDateFormat mDateformat = new SimpleDateFormat(format);
return mDateformat.format(mData);
}
private static String getWeek(Date date) {
String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (week_index < 0) {
week_index = 0;
}
return weeks[week_index];
}
/**
* @return yyyy-MM-dd HH:mm:ss.SSS 带毫秒的完整格式的时间
* @brief 获得包含年的完整时间
*/
public static String getMillionTimeAll() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINA).format(new Date());
}
}