1 package com.aone.foottalk.common;
2
3 import java.text.ParsePosition;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.Locale;
8
9 public class Demo {
10 /**
11 * 获取系统时间Date
12 * @return
13 */
14 public static Date systemtime() {
15 Date date = new Date();
16 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
17 String dateString = formatter.format(date);
18 ParsePosition pos = new ParsePosition(0);
19 Date currenttime = formatter.parse(dateString, pos);
20 return currenttime;
21 }
22 /**
23 * 获取系统时间long
24 * @return
25 */
26 public static long systemtimeLong() {
27 return new Date().getTime();
28 }
29 /**
30 * 获得当天零时零分零秒
31 * @return
32 */
33 public static Date initDateByDay(){
34 Calendar calendar = Calendar.getInstance();
35 calendar.setTime(new Date());
36 calendar.set(Calendar.HOUR, 0);
37 calendar.set(Calendar.MINUTE, 0);
38 calendar.set(Calendar.SECOND, 0);
39 return calendar.getTime();
40 }
41 /**
42 * 转换数据库日期格式
43 * @param object
44 * @return
45 */
46 public static String formateDbDate(Date object) {
47 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
48 String dateString = formatter.format(object);
49 return dateString;
50 }
51 /**
52 * 转换数据库long型日期至字符串类型
53 * @param millsec
54 * @return
55 */
56 public static String formateMillsecDateToStr(long millsec){
57 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
58 Date date= new Date(millsec);
59 return formatter.format(date);
60 }
61 /**
62 * 获取week
63 * @param date
64 * @return
65 */
66 public static String getWeek(Date date) {
67 String[] weeks = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
68 Calendar cal = Calendar.getInstance();
69 cal.setTime(date);
70 int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
71 if (week_index < 0) {
72 week_index = 0;
73 }
74 return weeks[week_index];
75 }
76 /**
77 * 查询当前时间N个小时后的时间
78 * @return
79 */
80 public static String getCurrentTimeAfterNTime(long time, int n) {
81 Calendar dar = Calendar.getInstance();
82 SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
83 dar.setTimeInMillis(time);
84 dar.add(Calendar.HOUR_OF_DAY, n);
85 return dft.format(dar.getTime());
86 }
87 /**
88 * 查询当前时间n秒后的时间
89 * @param n
90 * @return
91 */
92 public static long getCurrentTimeAfterNTime(long n) {
93 long currentTime = System.currentTimeMillis() ;
94 currentTime += n*1000;
95 return currentTime;
96 }
97 /**
98 * 获取昨天/明天的时间
99 * @param type 1:昨天 2:明天
100 * @return
101 */
102 public static String getYesTomDayDate(int type) {
103 Calendar cal = Calendar.getInstance();
104 if (type == 1){
105 cal.add(Calendar.DATE, -1);
106 }else if (type == 2){
107 cal.add(Calendar.DATE, 1);
108 }
109 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
110 String time = formatter.format(cal.getTime()).toString();
111 return time;
112 }
113 /**
114 * 当前系统n秒前的时间(n随机)
115 * @param n
116 * @return
117 */
118 public static String getCurrentTimeBeforTime() {
119 int ms=(int)(1+Math.random()*(60-1+1));
120 SimpleDateFormat fd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
121 Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
122 calendar.add(Calendar.SECOND, -ms);//当前系统时间 的 前10 秒的时间
123 String tensecondago = fd.format(calendar.getTime());
124 return tensecondago;
125 }
126 /**
127 * 获取当前时间前3分钟
128 * @return
129 */
130 public static String getCurrentTime(){
131 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
132 Calendar beforeTime = Calendar.getInstance();
133 beforeTime.add(Calendar.MINUTE, -2);
134 // 3分钟之前的时间
135 Date beforeD = beforeTime.getTime();
136 String time = sdf.format(beforeD);
137 return time;
138 }
139 /**
140 * 判断选择的日期是否是本周 (当前周)
141 * @param time
142 * @return
143 */
144 public static boolean isThisWeek(long time){
145 Calendar calendar = Calendar.getInstance();
146 int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
147 calendar.setTime(new Date(time));
148 int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
149 if(paramWeek==currentWeek){
150 return true;
151 }
152 return false;
153 }
154 /**
155 * 获取本周的开始时间
156 * @return
157 */
158 @SuppressWarnings("unused")
159 public static Date getBeginDayOfWeek() {
160 Date date = new Date();
161 if (date == null) {
162 return null;
163 }
164 Calendar cal = Calendar.getInstance();
165 cal.setTime(date);
166 int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
167 if (dayofweek == 1) {
168 dayofweek += 7;
169 }
170 cal.add(Calendar.DATE, 2 - dayofweek);
171 return cal.getTime();
172 }
173 /**
174 * 获取本周的结束时间
175 * @return
176 */
177 public static Date getEndDayOfWeek(){
178 Calendar cal = Calendar.getInstance();
179 cal.setTime(getBeginDayOfWeek());
180 cal.add(Calendar.DAY_OF_WEEK, 6);
181 Date weekEndSta = cal.getTime();
182 return weekEndSta;
183 }
184 /**
185 * 获得本周日24点时间
186 * @return
187 */
188 public static Date getTimesWeeknight() {
189 Calendar cal = Calendar.getInstance();
190 cal.setTime(getTimesWeekmorning());
191 cal.add(Calendar.DAY_OF_WEEK, 7);
192 return cal.getTime();
193 }
194
195 /**
196 * 获得本周一0点时间
197 * @return
198 */
199 public static Date getTimesWeekmorning() {
200 Calendar cal = Calendar.getInstance();
201 cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
202 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
203 return cal.getTime();
204 }
205 }