各种获取时间的⽅法包含各类时间格式今天介绍了简单的东西,但没遇到肯定不会,⽐如我啦!
也就是如何获取当前时间,可以设置时间格式哦!
具体的看代码就懂了
1//默认⽇期是当前⽇期
2        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置⽇期格式
3        rzsj.setText(df.format(new Date()));
很简单吧!
设置时间格式有以下⼏种:
看代码吧!你会⼤吃⼀惊,有很多有关时间的获取⽅法哦!
1.获取现在时间@return返回时间类型 yyyy-MM-dd HH:mm:ss
1/**
2197  * 获取现在时间
3198  *
4199  * @return返回时间类型 yyyy-MM-dd HH:mm:ss
5200  */
6 201  public static Date getNowDate() {
7 202  Date currentTime = new Date();
8 203  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
9 204  String dateString = formatter.format(currentTime);
10 205  ParsePosition pos = new ParsePosition(8);
11 206  Date currentTime_2 = formatter.parse(dateString, pos);
12 207  return currentTime_2;
13 208  }
2.获取现在时间  @return返回短时间格式 yyyy-MM-dd 5 214
1/**
2211  * 获取现在时间
3212  *
4213  * @return返回短时间格式 yyyy-MM-dd
5214  */
6 215  public static Date getNowDateShort() {
7 216  Date currentTime = new Date();
8 217  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
9 218  String dateString = formatter.format(currentTime);
10 219  ParsePosition pos = new ParsePosition(8);
11 220  Date currentTime_2 = formatter.parse(dateString, pos);
12 221  return currentTime_2;
13 222  }
3.获取现在时间 @return返回字符串格式 yyyy-MM-dd HH:mm:ss
1/**
2225  * 获取现在时间
3226  *
4227  * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
5228  */
6 229  public static String getStringDate() {
7 230  Date currentTime = new Date();
8 231  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
9 232  String dateString = formatter.format(currentTime);
10 233  return dateString;
11 234  }
4.
1/**
2237  * 获取现在时间
3238  *
4239  * @return返回短时间字符串格式yyyy-MM-dd
5240  */
6 241  public static String getStringDateShort() {
7 242  Date currentTime = new Date();
8 243  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
9 244  String dateString = formatter.format(currentTime);
10 245  return dateString;
11 246  }
1/**
2249  * 获取时间⼩时:分;秒 HH:mm:ss
3250  *
4251  * @return
5252  */
6 253  public static String getTimeShort() {
7 254  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
8 255  Date currentTime = new Date();
9 256  String dateString = formatter.format(currentTime);
10 257  return dateString;
11 258  }
6.将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
1 260  /**
2261  * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
3262  *
4263  * @param strDate
5264  * @return
6265  */
7 266  public static Date strToDateLong(String strDate) {
8 267  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
9 268  ParsePosition pos = new ParsePosition(0);
10 269  Date strtodate = formatter.parse(strDate, pos);
11 270  return strtodate;
12 271  }
7.将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
1/**
2274  * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
3275  *
4276  * @param dateDate
5277  * @return
6278  */
7 279  public static String dateToStrLong(java.util.Date dateDate) {
8 280  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
9 281  String dateString = formatter.format(dateDate);
10 282  return dateString;
11 283  }
8.将短时间格式时间转换为字符串 yyyy-MM-dd
1/**
2286  * 将短时间格式时间转换为字符串 yyyy-MM-dd
3287  *
4288  * @param dateDate
5289  * @param k
6290  * @return
7291  */
8 292  public static String dateToStr(java.util.Date dateDate) {
9 293  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
10 294  String dateString = formatter.format(dateDate);
11 295  return dateString;
12 296  }
9.将短时间格式字符串转换为时间 yyyy-MM-dd
1/**
2299  * 将短时间格式字符串转换为时间 yyyy-MM-dd
3300  *
4301  * @param strDate
5302  * @return
6303  */
7 304  public static Date strToDate(String strDate) {
8 305  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
9 306  ParsePosition pos = new ParsePosition(0);
10 307  Date strtodate = formatter.parse(strDate, pos);
11 308  return strtodate;
12 309  }
13 310
10.还有⼀些⼤家⾃⼰看看吧!不难看看到时候直接⽤就可以了
188
189做成⽅法
190
191import java.util.*;
*;
193import java.util.Calendar;
194
312  * 得到现在时间
313  *
314  * @return
315*/
316public static Date getNow() {
317  Date currentTime = new Date();
318return currentTime;
319  }
320
321/**
322  * 提取⼀个⽉中的最后⼀天
323  *
324  * @param day
325  * @return
326*/
327public static Date getLastDate(long day) {
328  Date date = new Date();
329long date_3_hm = Time() - 3600000 * 34 * day;
330  Date date_3_hm_date = new Date(date_3_hm);
331return date_3_hm_date;
332  }
333
334/**
335  * 得到现在时间
336  *
337  * @return字符串 yyyyMMdd HHmmss
338*/
339public static String getStringToday() {
340  Date currentTime = new Date();
341  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
342  String dateString = formatter.format(currentTime);
343return dateString;
344  }
345
346/**
347  * 得到现在⼩时
348*/
349public static String getHour() {
350  Date currentTime = new Date();
351  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
352  String dateString = formatter.format(currentTime);
353  String hour;
354  hour = dateString.substring(11, 13);
355return hour;
356  }
357
358/**
359  * 得到现在分钟
360  *
361  * @return
362*/
363public static String getTime() {
364  Date currentTime = new Date();
365  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
366  String dateString = formatter.format(currentTime);
367  String min;
368  min = dateString.substring(14, 16);
369return min;
370  }
371
372/**
373  * 根据⽤户传⼊的时间表⽰格式,返回当前时间的格式如果是yyyyMMdd,注意字母y不能⼤写。374  *
375  * @param sformat
376  *            yyyyMMddhhmmss
377  * @return
378*/
379public static String getUserDate(String sformat) {
380  Date currentTime = new Date();
381  SimpleDateFormat formatter = new SimpleDateFormat(sformat);
382  String dateString = formatter.format(currentTime);
383return dateString;
384  }
385
386/**
387  * ⼆个⼩时时间间的差值,必须保证⼆个时间都是"HH:MM"的格式,返回字符型的分钟
388*/
389public static String getTwoHour(String st1, String st2) {
390  String[] kk = null;
391  String[] jj = null;
392  kk = st1.split(":");
393  jj = st2.split(":");
396else {
397double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;
398double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;
399if ((y - u) > 0)
400return y - u + "";
401else
402return "0";
403  }
404  }
405
406/**
407  * 得到⼆个⽇期间的间隔天数
408*/
409public static String getTwoDay(String sj1, String sj2) {
410  SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
411long day = 0;
412try {
413    java.util.Date date = myFormatter.parse(sj1);
414    java.util.Date mydate = myFormatter.parse(sj2);
415    day = (Time() - Time()) / (24 * 60 * 60 * 1000);
416  } catch (Exception e) {
417return "";
418  }
419return day + "";
420  }
421
422/**
423  * 时间前推或后推分钟,其中JJ表⽰分钟.
424*/
425public static String getPreTime(String sj1, String jj) {
426  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
427  String mydate1 = "";
428try {
429    Date date1 = format.parse(sj1);
430long Time = (Time() / 1000) + Integer.parseInt(jj) * 60;
431    date1.setTime(Time * 1000);
432    mydate1 = format.format(date1);
433  } catch (Exception e) {
434  }
435return mydate1;
436  }
437
438/**
439  * 得到⼀个时间延后或前移⼏天的时间,nowdate为时间,delay为前移或后延的天数
440*/
441public static String getNextDay(String nowdate, String delay) {
442try{
443  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
444  String mdate = "";
445  Date d = strToDate(nowdate);
446long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
447  d.setTime(myTime * 1000);
448  mdate = format.format(d);
449return mdate;
450  }catch(Exception e){
451return "";
452  }
453  }
454
455/**
456  * 判断是否润年
457  *
458  * @param ddate
459  * @return
460*/
461public static boolean isLeapYear(String ddate) {
462
463/**
464  * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年465  * 3.能被4整除同时能被100整除则不是闰年
466*/
467  Date d = strToDate(ddate);
468  GregorianCalendar gc = (GregorianCalendar) Instance();
469  gc.setTime(d);
470int year = gc.get(Calendar.YEAR);
471if ((year % 400) == 0)
472return true;
473else if ((year % 4) == 0) {
474if ((year % 100) == 0)
475return false;
476else
477return true;
480  }
481
482/**
483  * 返回美国时间格式 26 Apr 2006
484  *
485  * @param str
486  * @return
487*/
488public static String getEDate(String str) {
489  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
490  ParsePosition pos = new ParsePosition(0);
491  Date strtodate = formatter.parse(str, pos);
492  String j = String();
493  String[] k = j.split(" ");
494return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
495  }
496
497/**
498  * 获取⼀个⽉的最后⼀天
499  *
500  * @param dat
501  * @return
502*/
503public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
504  String str = dat.substring(0, 8);
505  String month = dat.substring(5, 7);
506int mon = Integer.parseInt(month);
507if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) { 508    str += "31";
509  } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
510    str += "30";
511  } else {
512if (isLeapYear(dat)) {
513    str += "29";
514    } else {
515    str += "28";
516    }
517  }
518return str;
519  }
520
521/**
522  * 判断⼆个时间是否在同⼀个周
523  *
524  * @param date1
525  * @param date2
526  * @return
527*/
528public static boolean isSameWeekDates(Date date1, Date date2) {
529  Calendar cal1 = Instance();
530  Calendar cal2 = Instance();
531  cal1.setTime(date1);
532  cal2.setTime(date2);
533int subYear = (Calendar.YEAR) - (Calendar.YEAR);
534if (0 == subYear) {
535if ((Calendar.WEEK_OF_YEAR) == (Calendar.WEEK_OF_YEAR))
536return true;
537  } else if (1 == subYear && 11 == (Calendar.MONTH)) {
538// 如果12⽉的最后⼀周横跨来年第⼀周的话则最后⼀周即算做来年的第⼀周
539if ((Calendar.WEEK_OF_YEAR) == (Calendar.WEEK_OF_YEAR))
540return true;
541  } else if (-1 == subYear && 11 == (Calendar.MONTH)) {
542if ((Calendar.WEEK_OF_YEAR) == (Calendar.WEEK_OF_YEAR))
543return true;
544  }
545return false;
546  }
java时间日期格式转换547
548/**
549  * 产⽣周序列,即得到当前时间所在的年度是第⼏周
550  *
551  * @return
552*/
553public static String getSeqWeek() {
554  Calendar c = Instance(Locale.CHINA);
555  String week = (Calendar.WEEK_OF_YEAR));
556if (week.length() == 1)
557    week = "0" + week;
558  String year = (Calendar.YEAR));
559return year + week;
560  }
561

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。