今天介绍了简单的东西,但没遇到肯定不会,比如我啦!
也就是如何获取当前时间,可以设置时间格式哦!
具体的看代码就懂了
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 /** 2 197 * 获取现在时间 3 198 * 4 199 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss 5 200 */ 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 /** 2 211 * 获取现在时间 3 212 * 4 213 * @return返回短时间格式 yyyy-MM-dd 5 214 */ 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 /** 2 225 * 获取现在时间 3 226 * 4 227 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss 5 228 */ 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 /** 2 237 * 获取现在时间 3 238 * 4 239 * @return 返回短时间字符串格式yyyy-MM-dd 5 240 */ 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 }
5.* 获取时间 小时:分;秒 HH:mm:ss
1 /** 2 249 * 获取时间 小时:分;秒 HH:mm:ss 3 250 * 4 251 * @return 5 252 */ 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 /** 2 261 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss 3 262 * 4 263 * @param strDate 5 264 * @return 6 265 */ 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 /** 2 274 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss 3 275 * 4 276 * @param dateDate 5 277 * @return 6 278 */ 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 /** 2 286 * 将短时间格式时间转换为字符串 yyyy-MM-dd 3 287 * 4 288 * @param dateDate 5 289 * @param k 6 290 * @return 7 291 */ 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 /** 2 299 * 将短时间格式字符串转换为时间 yyyy-MM-dd 3 300 * 4 301 * @param strDate 5 302 * @return 6 303 */ 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 191 import java.util.*;192 import java.text.*;193 import java.util.Calendar;194 195 public class VeDate { 311 /**312 * 得到现在时间313 * 314 * @return315 */316 public static Date getNow() {317 Date currentTime = new Date();318 return currentTime;319 }320 321 /**322 * 提取一个月中的最后一天323 * 324 * @param day325 * @return326 */327 public static Date getLastDate(long day) {328 Date date = new Date();329 long date_3_hm = date.getTime() - 3600000 * 34 * day;330 Date date_3_hm_date = new Date(date_3_hm);331 return date_3_hm_date;332 }333 334 /**335 * 得到现在时间336 * 337 * @return 字符串 yyyyMMdd HHmmss338 */339 public static String getStringToday() {340 Date currentTime = new Date();341 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");342 String dateString = formatter.format(currentTime);343 return dateString;344 }345 346 /**347 * 得到现在小时348 */349 public 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);355 return hour;356 }357 358 /**359 * 得到现在分钟360 * 361 * @return362 */363 public 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);369 return min;370 }371 372 /**373 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。374 * 375 * @param sformat376 * yyyyMMddhhmmss377 * @return378 */379 public static String getUserDate(String sformat) {380 Date currentTime = new Date();381 SimpleDateFormat formatter = new SimpleDateFormat(sformat);382 String dateString = formatter.format(currentTime);383 return dateString;384 }385 386 /**387 * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟388 */389 public static String getTwoHour(String st1, String st2) {390 String[] kk = null;391 String[] jj = null;392 kk = st1.split(":");393 jj = st2.split(":");394 if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))395 return "0";396 else {397 double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;398 double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;399 if ((y - u) > 0)400 return y - u + "";401 else402 return "0";403 }404 }405 406 /**407 * 得到二个日期间的间隔天数408 */409 public static String getTwoDay(String sj1, String sj2) {410 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");411 long day = 0;412 try {413 java.util.Date date = myFormatter.parse(sj1);414 java.util.Date mydate = myFormatter.parse(sj2);415 day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);416 } catch (Exception e) {417 return "";418 }419 return day + "";420 }421 422 /**423 * 时间前推或后推分钟,其中JJ表示分钟.424 */425 public static String getPreTime(String sj1, String jj) {426 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");427 String mydate1 = "";428 try {429 Date date1 = format.parse(sj1);430 long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;431 date1.setTime(Time * 1000);432 mydate1 = format.format(date1);433 } catch (Exception e) {434 }435 return mydate1;436 }437 438 /**439 * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数440 */441 public static String getNextDay(String nowdate, String delay) {442 try{443 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");444 String mdate = "";445 Date d = strToDate(nowdate);446 long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;447 d.setTime(myTime * 1000);448 mdate = format.format(d);449 return mdate;450 }catch(Exception e){451 return "";452 }453 }454 455 /**456 * 判断是否润年457 * 458 * @param ddate459 * @return460 */461 public 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) Calendar.getInstance();469 gc.setTime(d);470 int year = gc.get(Calendar.YEAR);471 if ((year % 400) == 0)472 return true;473 else if ((year % 4) == 0) {474 if ((year % 100) == 0)475 return false;476 else477 return true;478 } else479 return false;480 }481 482 /**483 * 返回美国时间格式 26 Apr 2006484 * 485 * @param str486 * @return487 */488 public 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 = strtodate.toString();493 String[] k = j.split(" ");494 return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);495 }496 497 /**498 * 获取一个月的最后一天499 * 500 * @param dat501 * @return502 */503 public static String getEndDateOfMonth(String dat) { // yyyy-MM-dd504 String str = dat.substring(0, 8);505 String month = dat.substring(5, 7);506 int mon = Integer.parseInt(month);507 if (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 {512 if (isLeapYear(dat)) {513 str += "29";514 } else {515 str += "28";516 }517 }518 return str;519 }520 521 /**522 * 判断二个时间是否在同一个周523 * 524 * @param date1525 * @param date2526 * @return527 */528 public static boolean isSameWeekDates(Date date1, Date date2) {529 Calendar cal1 = Calendar.getInstance();530 Calendar cal2 = Calendar.getInstance();531 cal1.setTime(date1);532 cal2.setTime(date2);533 int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);534 if (0 == subYear) {535 if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))536 return true;537 } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {538 // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周539 if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))540 return true;541 } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {542 if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))543 return true;544 }545 return false;546 }547 548 /**549 * 产生周序列,即得到当前时间所在的年度是第几周550 * 551 * @return552 */553 public static String getSeqWeek() {554 Calendar c = Calendar.getInstance(Locale.CHINA);555 String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));556 if (week.length() == 1)557 week = "0" + week;558 String year = Integer.toString(c.get(Calendar.YEAR));559 return year + week;560 }561 562 /**563 * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号564 * 565 * @param sdate566 * @param num567 * @return568 */569 public static String getWeek(String sdate, String num) {570 // 再转换为时间571 Date dd = VeDate.strToDate(sdate);572 Calendar c = Calendar.getInstance();573 c.setTime(dd);574 if (num.equals("1")) // 返回星期一所在的日期575 c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);576 else if (num.equals("2")) // 返回星期二所在的日期577 c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);578 else if (num.equals("3")) // 返回星期三所在的日期579 c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);580 else if (num.equals("4")) // 返回星期四所在的日期581 c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);582 else if (num.equals("5")) // 返回星期五所在的日期583 c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);584 else if (num.equals("6")) // 返回星期六所在的日期585 c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);586 else if (num.equals("0")) // 返回星期日所在的日期587 c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);588 return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());589 }590 591 /**592 * 根据一个日期,返回是星期几的字符串593 * 594 * @param sdate595 * @return596 */597 public static String getWeek(String sdate) {598 // 再转换为时间599 Date date = VeDate.strToDate(sdate);600 Calendar c = Calendar.getInstance();601 c.setTime(date);602 // int hour=c.get(Calendar.DAY_OF_WEEK);603 // hour中存的就是星期几了,其范围 1~7604 // 1=星期日 7=星期六,其他类推605 return new SimpleDateFormat("EEEE").format(c.getTime());606 }607 public static String getWeekStr(String sdate){608 String str = "";609 str = VeDate.getWeek(sdate);610 if("1".equals(str)){611 str = "星期日";612 }else if("2".equals(str)){613 str = "星期一";614 }else if("3".equals(str)){615 str = "星期二";616 }else if("4".equals(str)){617 str = "星期三";618 }else if("5".equals(str)){619 str = "星期四";620 }else if("6".equals(str)){621 str = "星期五";622 }else if("7".equals(str)){623 str = "星期六";624 }625 return str;626 }627 628 /**629 * 两个时间之间的天数630 * 631 * @param date1632 * @param date2633 * @return634 */635 public static long getDays(String date1, String date2) {636 if (date1 == null || date1.equals(""))637 return 0;638 if (date2 == null || date2.equals(""))639 return 0;640 // 转换为标准时间641 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");642 java.util.Date date = null;643 java.util.Date mydate = null;644 try {645 date = myFormatter.parse(date1);646 mydate = myFormatter.parse(date2);647 } catch (Exception e) {648 }649 long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);650 return day;651 }652 653 /**654 * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间655 * 此函数返回该日历第一行星期日所在的日期656 * 657 * @param sdate658 * @return659 */660 public static String getNowMonth(String sdate) {661 // 取该时间所在月的一号662 sdate = sdate.substring(0, 8) + "01";663 664 // 得到这个月的1号是星期几665 Date date = VeDate.strToDate(sdate);666 Calendar c = Calendar.getInstance();667 c.setTime(date);668 int u = c.get(Calendar.DAY_OF_WEEK);669 String newday = VeDate.getNextDay(sdate, (1 - u) + "");670 return newday;671 }672 673 /**674 * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数675 * 676 * @param k677 * 表示是取几位随机数,可以自己定678 */679 680 public static String getNo(int k) {681 682 return getUserDate("yyyyMMddhhmmss") + getRandom(k);683 }684 685 /**686 * 返回一个随机数687 * 688 * @param i689 * @return690 */691 public static String getRandom(int i) {692 Random jjj = new Random();693 // int suiJiShu = jjj.nextInt(9);694 if (i == 0)695 return "";696 String jj = "";697 for (int k = 0; k < i; k++) {698 jj = jj + jjj.nextInt(9);699 }700 return jj;701 }702 703 /**704 * 705 * @param args706 */707 public static boolean RightDate(String date) {708 709 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");710 ;711 if (date == null)712 return false;713 if (date.length() > 10) {714 sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");715 } else {716 sdf = new SimpleDateFormat("yyyy-MM-dd");717 }718 try {719 sdf.parse(date);720 } catch (ParseException pe) {721 return false;722 }723 return true;724 }725 726 /***************************************************************************727 * //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1728 * 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回729 **************************************************************************/730 public static String getStringDateMonth(String sdate, String nd, String yf, String rq, String format) {731 Date currentTime = new Date();732 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");733 String dateString = formatter.format(currentTime);734 String s_nd = dateString.substring(0, 4); // 年份735 String s_yf = dateString.substring(5, 7); // 月份736 String s_rq = dateString.substring(8, 10); // 日期737 String sreturn = "";738 roc.util.MyChar mc = new roc.util.MyChar();739 if (sdate == null || sdate.equals("") || !mc.Isdate(sdate)) { // 处理空值情况740 if (nd.equals("1")) {741 sreturn = s_nd;742 // 处理间隔符743 if (format.equals("1"))744 sreturn = sreturn + "年";745 else if (format.equals("2"))746 sreturn = sreturn + "-";747 else if (format.equals("3"))748 sreturn = sreturn + "/";749 else if (format.equals("5"))750 sreturn = sreturn + ".";751 }752 // 处理月份753 if (yf.equals("1")) {754 sreturn = sreturn + s_yf;755 if (format.equals("1"))756 sreturn = sreturn + "月";757 else if (format.equals("2"))758 sreturn = sreturn + "-";759 else if (format.equals("3"))760 sreturn = sreturn + "/";761 else if (format.equals("5"))762 sreturn = sreturn + ".";763 }764 // 处理日期765 if (rq.equals("1")) {766 sreturn = sreturn + s_rq;767 if (format.equals("1"))768 sreturn = sreturn + "日";769 }770 } else {771 // 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式772 sdate = roc.util.RocDate.getOKDate(sdate);773 s_nd = sdate.substring(0, 4); // 年份774 s_yf = sdate.substring(5, 7); // 月份775 s_rq = sdate.substring(8, 10); // 日期776 if (nd.equals("1")) {777 sreturn = s_nd;778 // 处理间隔符779 if (format.equals("1"))780 sreturn = sreturn + "年";781 else if (format.equals("2"))782 sreturn = sreturn + "-";783 else if (format.equals("3"))784 sreturn = sreturn + "/";785 else if (format.equals("5"))786 sreturn = sreturn + ".";787 }788 // 处理月份789 if (yf.equals("1")) {790 sreturn = sreturn + s_yf;791 if (format.equals("1"))792 sreturn = sreturn + "月";793 else if (format.equals("2"))794 sreturn = sreturn + "-";795 else if (format.equals("3"))796 sreturn = sreturn + "/";797 else if (format.equals("5"))798 sreturn = sreturn + ".";799 }800 // 处理日期801 if (rq.equals("1")) {802 sreturn = sreturn + s_rq;803 if (format.equals("1"))804 sreturn = sreturn + "日";805 }806 }807 return sreturn;808 }809 810 public static String getNextMonthDay(String sdate, int m) {811 sdate = getOKDate(sdate);812 int year = Integer.parseInt(sdate.substring(0, 4));813 int month = Integer.parseInt(sdate.substring(5, 7));814 month = month + m;815 if (month < 0) {816 month = month + 12;817 year = year - 1;818 } else if (month > 12) {819 month = month - 12;820 year = year + 1;821 }822 String smonth = "";823 if (month < 10)824 smonth = "0" + month;825 else826 smonth = "" + month;827 return year + "-" + smonth + "-10";828 }829 830 public static String getOKDate(String sdate) {831 if (sdate == null || sdate.equals(""))832 return getStringDateShort();833 834 if (!VeStr.Isdate(sdate)) {835 sdate = getStringDateShort();836 }837 // 将“/”转换为“-”838 sdate = VeStr.Replace(sdate, "/", "-");839 // 如果只有8位长度,则要进行转换840 if (sdate.length() == 8)841 sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);842 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");843 ParsePosition pos = new ParsePosition(0);844 Date strtodate = formatter.parse(sdate, pos);845 String dateString = formatter.format(strtodate);846 return dateString;847 }848 849 public static void main(String[] args) throws Exception {850 try {851 //System.out.print(Integer.valueOf(getTwoDay("2006-11-03 12:22:10", "2006-11-02 11:22:09")));852 } catch (Exception e) {853 throw new Exception();854 }855 //System.out.println("sss");856 }