java时间戳与日期相互转换工具详解

时间:2022-09-09 19:52:41

本文为大家分享了java日期时间戳相互转换大全,供大家参考,具体内容如下

?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package com.crm.util;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
/**
 * @author DingJiaCheng
 * */
public class DateFormatUtil {
   
  /**
   * 时间戳转日期
   * @param ms
   * @return
   */
  public static Date transForDate(Integer ms){
    if(ms==null){
      ms=0;
    }
    long msl=(long)ms*1000;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date temp=null;
    if(ms!=null){
      try {
        String str=sdf.format(msl);
        temp=sdf.parse(str);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return temp;
  }
   
  /**
   * 获取晚上9点半的时间戳
   *
   * @return
   */
  public static int getTimes(int day, int hour, int minute) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, day);
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.MILLISECOND, 0);
    return (int) (cal.getTimeInMillis() / 1000);
  }
   
  /**
   * 获取当前时间往上的整点时间
   *
   * @return
   */
  public static int getIntegralTime() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR_OF_DAY, 1);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (int) (cal.getTimeInMillis() / 1000);
  }
   
  public static int getIntegralTimeEnd() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 24);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return (int) (cal.getTimeInMillis() / 1000);
  }
   
  /**
   * 时间戳转日期
   * @param ms
   * @return
   */
  public static Date transForDate3(Integer ms){
    if(ms==null){
      ms=0;
    }
    long msl=(long)ms*1000;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date temp=null;
    if(ms!=null){
      try {
        String str=sdf.format(msl);
        temp=sdf.parse(str);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return temp;
  }
  /**
   * 时间戳转日期
   * @param ms
   * @return
   */
  public static Date transForDate(Long ms){
    if(ms==null){
      ms=(long)0;
    }
    long msl=(long)ms*1000;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date temp=null;
    if(ms!=null){
      try {
        String str=sdf.format(msl);
        temp=sdf.parse(str);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return temp;
  }
   
   
  public static String transForDate1(Integer ms){
    String str = "";
    if(ms!=null){
      long msl=(long)ms*1000;
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       
      if(ms!=null){
        try {
          str=sdf.format(msl);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }    
    return str;
  }
  public static String transForDate2(Integer ms){
    String str = "";
    if(ms!=null){
      long msl=(long)ms*1000;
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
       
      if(ms!=null){
        try {
          str=sdf.format(msl);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }    
    return str;
  }
   
  public static String transForDate4(Integer ms){
    String str = "";
    if(ms!=null){
      long msl=(long)ms*1000;
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");
       
      if(ms!=null){
        try {
          str=sdf.format(msl);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }    
    return str;
  }
   
   
  public static String transForDate5(Integer ms){
    String str = "";
    if(ms!=null){
      long msl=(long)ms*1000;
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
       
      if(ms!=null){
        try {
          str=sdf.format(msl);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }    
    return str;
  }
   
  public static String transForDateInChinese(Integer ms){
    String str = "";
    if(ms!=null){
      long msl=(long)ms*1000;
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
       
      if(ms!=null){
        try {
          str=sdf.format(msl);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }    
    return str;
  }
   
  /**
   * 日期转时间戳
   * @param date
   * @return
   */
  public static Integer transForMilliSecond(Date date){
    if(date==null) return null;
    return (int)(date.getTime()/1000);
  }
   
  /**
   * 获取当前时间戳
   * @return
   */
  public static Integer currentTimeStamp(){
    return (int)(System.currentTimeMillis()/1000);
  }
   
  /**
   * 日期字符串转时间戳
   * @param dateStr
   * @return
   */
  public static Integer transForMilliSecond(String dateStr){
    Date date = DateFormatUtil.formatDate(dateStr);
    return date == null ? null : DateFormatUtil.transForMilliSecond(date);
  }
  /**
   * 日期字符串转时间戳
   * @param dateStr
   * @return
   */
  public static Integer transForMilliSecond(String dateStr,String format){
    Date date = DateFormatUtil.formatDate(dateStr,format);
    return date == null ? null : DateFormatUtil.transForMilliSecond(date);
  }
  /**
   * 日期字符串转时间戳
   * @param dateStr
   * @param 格式 如"yyyy-mm-dd"
   * @return
   */
  public static Integer transForMilliSecondByTim(String dateStr,String tim){
    SimpleDateFormat sdf=new SimpleDateFormat(tim);
    Date date =null;
    try {
      date = sdf.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date == null ? null : DateFormatUtil.transForMilliSecond(date);
  }
  /**
   * 字符串转日期,格式为:"yyyy-MM-dd HH:mm:ss"
   * @param dateStr
   * @return
   */
  public static Date formatDate(String dateStr){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date result=null;
    try {
      result = sdf.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return result;
  }
  /**
   * 字符串转日期,格式为:"yyyy-MM-dd HH:mm:ss"
   * @param dateStr
   * @return
   */
  public static Date formatDate(String dateStr,String format){
    SimpleDateFormat sdf=new SimpleDateFormat(format);
    Date result=null;
    try {
      result = sdf.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return result;
  }
  /**
   * 日期转字符串
   * @param date
   * @return
   */
  public static String formatDate(Date date){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String result=null;
    result = sdf.format(date);
    return result;
  }
  /**
   * 日期转字符串
   * @param date
   * @return
   */
  public static String formatDate(Date date,String format){
    SimpleDateFormat sdf=new SimpleDateFormat(format);
    String result=null;
    result = sdf.format(date);
    return result;
  }
  /**
   * 时间戳格式化输出(httl模版用)
   *
   * @param ms    时间戳
   * @param format  格式化
   * @return
   */
  public static String transForDate(Integer ms, String format){
    String str = "";
    if(ms!=null){
      long msl=(long)ms*1000;
      SimpleDateFormat sdf=new SimpleDateFormat(format);
      if(!ms.equals(0)){
        try {
          str=sdf.format(msl);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    return str;
  }  
   
  /**
   * 取BigDecimal类型数的整数或小数部分(httl模版用)
   *
   * @param b 值
   * @param mode 模式 0取整 1去小数部分
   * @return
   */
  public static String splitBigDecimal(BigDecimal b, int mode) {
    DecimalFormat df = new DecimalFormat("0.00");
    String s = df.format(b);
    if(mode==0){
      return s.split("\\.")[0];
    }else {
      return "."+s.split("\\.")[1];
    }
  }
   
  /**
   * 计算两个日期之间差的天数(httl模版用)
   *
   * @param ts1  时间戳1
   * @param ts2  时间戳2
   * @return
   */
  public static int caculate2Days(Integer ts1, Integer ts2) {
    Date firstDate = DateFormatUtil.transForDate(ts1);
    Date secondDate = DateFormatUtil.transForDate(ts2);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(firstDate);
    int dayNum1 = calendar.get(Calendar.DAY_OF_YEAR);
    calendar.setTime(secondDate);
    int dayNum2 = calendar.get(Calendar.DAY_OF_YEAR);
    return Math.abs(dayNum1 - dayNum2);
  }
   
  /**
   * 给手机加密中间四位加星号
   *
   * @param mobile
   * @return
   */
  public String mobileSerect(String mobile){
    if(!StringUtils.isBlank(mobile)){
      int between = mobile.length()/2;
      mobile = mobile.substring(0, between-2)+"****"+mobile.substring(between+2, mobile.length());
    }
    return mobile;
  }
   
  /**
   * 给邮箱加密加星号
   *
   * @param email
   * @return
   */
  public String emailSerect(String email) {
    if(!StringUtils.isBlank(email)){
      int length = email.lastIndexOf("@");
      email = email.substring(0, 2)+"****"+email.substring(length-2, email.length());
    }
    return email;
  }
   
  /**
   * BigDecimal类型数据相加
   *
   * @param BigDecimal source
   * @param BigDecimal target
   * @return
   */
  public BigDecimal sumBigDicimal(BigDecimal source, BigDecimal target) {
    source = source.add(target);
    return source;
  }
   
  /**
   * BigDecimal类型数据相加
   *
   * @param BigDecimal source
   * @param BigDecimal target
   * @return
   */
  public BigDecimal sumBigDicimalAndDouble(BigDecimal source, Double target) {
    BigDecimal new_target = new BigDecimal(target);
    source = source.add(new_target);
    return source;
  }
   
  /**
   * BigDecimal类型数据相减
   *
   * @param BigDecimal source
   * @param BigDecimal target
   * @return
   */
  public BigDecimal subBigDicimal(BigDecimal source, BigDecimal target) {
    source = source.subtract(target);
    return source;
  }
   
  /**
   * 获取传入时间和当前时间的时间差
   * @return
   */
  public static Long getTimediff(int timeStamp){
    Date d1 = DateFormatUtil.transForDate(timeStamp);
    Date today = new Date();
    if(d1.getTime()<today.getTime()){
      return null;
    }
    return (d1.getTime()-today.getTime())/1000;
  }
 
  /**
   * 获取某周的第一天日期
   * @param week 0 当周 1 上一周 -1 下一周
   * @return
   */
  public static String weekFirstDay(int week){
    Calendar c1 = Calendar.getInstance();
    int dow = c1.get(Calendar.DAY_OF_WEEK);
    c1.add(Calendar.DATE, -dow-7*(week-1)-5 );
    String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());
    return d1+" 00:00:00";
  }
   
  /**
   * 当前时间加一年
   */
  public static String addYear(int startTime){
    Date firstDate = DateFormatUtil.transForDate(startTime);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(firstDate);
    calendar.add(Calendar.YEAR,1);
    String d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    return d1;
  }
   
  /**
   * 获取某周的最后一天日期
   * @param week
   * @return
   */
  public static String weekLastDay(int week){
    Calendar c1 = Calendar.getInstance();
    int dow = c1.get(Calendar.DAY_OF_WEEK);
    c1.add(Calendar.DATE, -dow-7*(week-1)+1);
    String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime());
    return d1+" 23:59:59";
  }  
   
  /**
   * 和当前时间比对
   * @return
   */
  public static boolean greaterThanNow(int timeStamp){
    Date d1 = DateFormatUtil.transForDate(timeStamp);
    Date today = new Date();
    if(d1.getTime()>=today.getTime()){
      return true;
    }
    return false;
  }
 
   
   
  /**
   * HH:mm:ss格式时间转换为1970-01-01日的时间戳(也就是只有时间没有日期的情况要求使用时间戳表示时间)
   * @author DingJiaCheng
   * */
  public static int transFromTime(String time){
    return transForMilliSecond("1970-01-01 "+time,"yyyy-mm-dd HH:mm:ss");
  }
   
  /**
   * 时间戳转换为HH:mm:ss格式时间(日期去除)
   * @author DingJiaCheng
   * */
  public static String transToTime(int time){
    String s = new String(transForDate1(time));
    String ss[] = s.split(" ");
    return ss[1];
  }
   
  public static int transToChuo(String dateString){
    SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
    int res = 0;
    try {
      Date date=simpleDateFormat .parse(dateString);
      res = (int) date.getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return res;
  }
   
  public static void main(String[] args) {
     
    //System.out.println(getIntegralTimeEnd());
    System.out.println(transForDate2(transForMilliSecond("2015-02-25 00:00:00")));
    //System.out.println(transForMilliSecond("2016-01-25","yyyy-mm-dd"));
    //System.out.println(transForDate1(transForMilliSecond("1970-01-01 00:00:00","yyyy-mm-dd HH:mm:ss")));
    //System.out.println(currentTimeStamp());
    //System.out.println(transForDate(currentTimeStamp()));
    //System.out.println(new Date());
    //System.out.println(DateUtils.getDate());
    System.out.println(transFromTime("00:00:01"));
    System.out.println(transToTime(transFromTime("15:01:13")));
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.****.net/djc777/article/details/50904989