1 package com.ryd.stocktrader.monitor.util;
2
3 import java.sql.Timestamp;
4 import java.text.DateFormat;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
8 import java.util.Date;
9
10 /**
11 * Created by Administrator on 2016/7/27.
12 */
13 public class GetDayTime {
14 //当天零点时间戳
15 public static Long getDayBegin() {
16 Calendar cal = Calendar.getInstance();
17 cal.set(Calendar.HOUR_OF_DAY, 0);
18 cal.set(Calendar.SECOND, 0);
19 cal.set(Calendar.MINUTE, 0);
20 cal.set(Calendar.MILLISECOND, 001);
21 long time = new Timestamp(cal.getTimeInMillis()).getTime();
22 return time;
23 }
24
25 public static Long getDayEnd() {
26 long time = GetDayTime.getDayBegin() + 86400000;
27 return time;
28 }
29 //string 转 Unix时间戳
30 public static Long stringToUnixT(String time){
31 SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
32 Date date= null;
33 try {
34 date = simpleDateFormat.parse(time);
35 } catch (ParseException e) {
36 e.printStackTrace();
37 }
38 long timeStemp = date.getTime();
39 return timeStemp;
40 }
41 //string 转 Unix时间戳 (结束时间的24点)
42 public static Long stringToUnixTEnd(String time){
43 SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
44 Date date= null;
45 try {
46 date = simpleDateFormat.parse(time);
47 } catch (ParseException e) {
48 e.printStackTrace();
49 }
50 long timeStemp = date.getTime() + 86400000;
51 return timeStemp;
52 }
53 //当前日期
54 public static String nowDay(){
55 DateFormat d1 = DateFormat.getDateInstance();
56 Date now = new Date();
57 System.out.println(d1.format(now));
58 return d1.format(now);
59 }
60 //一周前的日期
61 public static String SevenDay(){
62 Date date = new Date();
63 int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
64 int month=Integer.parseInt(new SimpleDateFormat("MM").format(date));
65 int day=Integer.parseInt(new SimpleDateFormat("dd").format(date))-6;
66
67 if(day<1){
68 month-=1;
69 if(month==0){
70 year-=1;month=12;
71 }
72 if(month==4||month==6||month==9||month==11){
73 day=30+day;
74 }else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
75 {
76 day=31+day;
77 }else if(month==2){
78 if(year%400==0||(year %4==0&&year%100!=0))day=29+day;
79 else day=28+day;
80 }
81 }
82 String y = year+"";String m ="";String d ="";
83 if(month<10) m = "0"+month;
84 else m=month+"";
85 if(day<10) d = "0"+day;
86 else d = day+"";
87
88 return y+"-"+m+"-"+d;
89 }
90 //七天前的Unix时间戳
91 public static long SevenDayToUnixT(){
92 SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
93 Date date= null;
94 try {
95 date = simpleDateFormat.parse(SevenDay());
96 } catch (ParseException e) {
97 e.printStackTrace();
98 }
99 long timeStemp = date.getTime();
100 return timeStemp;
101 }
102 public static String lastMonth(){
103 int allMonth = 1; //allMonth为相差月份
104 Date date = new Date();
105 int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
106 int month=Integer.parseInt(new SimpleDateFormat("MM").format(date))-allMonth;
107 int day=Integer.parseInt(new SimpleDateFormat("dd").format(date));
108 if(month <= 0){
109 int yearFlag = (month*(-1))/12 + 1;
110 int monthFlag = (month *(-1))%12;
111 year -= yearFlag;
112 month=monthFlag*(-1) +12;
113 }
114 else if(day>28){
115 if(month==2){
116 if(year%400==0||(year %4==0&&year%100!=0)){
117 day=29;
118 }else day=28;
119 }else if((month==4||month==6||month==9||month==11)&&day==31){
120 day=30;
121 }
122 }
123 String y = year+"";String m ="";String d ="";
124 if(month<10) m = "0"+month;
125 else m=month+"";
126 if(day<10) d = "0"+day;
127 else d = day+"";
128
129 return y+"-"+m+"-"+d;
130 }
131 //当天11:30:00的时间戳
132 public static long cusTimeBefor(){
133 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
134 Date now=new Date();
135 String dStr = dateFormat.format(now)+" 11:30:00";
136 SimpleDateFormat dateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
137 long time = 0;
138 try {
139 time = dateFormat1.parse(dStr).getTime();
140 } catch (ParseException e) {
141 e.printStackTrace();
142 }
143 return time;
144 }
145 //当天13:00:00的时间戳
146 public static long cusTimelast(){
147 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
148 Date now=new Date();
149 String dStr = dateFormat.format(now)+" 13:00:00";
150 SimpleDateFormat dateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
151 long time = 0;
152 try {
153 time = dateFormat1.parse(dStr).getTime();
154 } catch (ParseException e) {
155 e.printStackTrace();
156 }
157 return time;
158 }
159 public static void main(String args []){
160 System.out.println(lastMonth());
161 }
162 }