zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java获取当月天数,指定年月的天数,指定日期获取对应星期 .详解编程语言

JAVA日期编程语言 详解 获取 指定 对应 天数
2023-06-13 09:20:42 时间
 public static int getCurrentMonthDay() {   Calendar a = Calendar.getInstance();   a.set(Calendar.DATE, 1);   a.roll(Calendar.DATE, -1);   int maxDate = a.get(Calendar.DATE);   return maxDate;   }     /**   * 根据 年、月 获取对应的月份 的 天数   */   public static int getDaysByYearMonth(int year, int month) {   Calendar a = Calendar.getInstance();   a.set(Calendar.YEAR, year);   a.set(Calendar.MONTH, month - 1);   a.set(Calendar.DATE, 1);   a.roll(Calendar.DATE, -1);   int maxDate = a.get(Calendar.DATE);   return maxDate;   }     /**   * 根据日期 找到对应日期的 星期几   *   * @param date 比如传参:2018-07-13 将返回“周五”   */   public static String getDayOfWeekByDate(String date) {   String dayOfweek =  -1    try {   SimpleDateFormat myFormatter = new SimpleDateFormat( yyyy-MM-dd    Date myDate = myFormatter.parse(date);   SimpleDateFormat formatter = new SimpleDateFormat( E    String str = formatter.format(myDate);   dayOfweek = str;   }   catch (Exception e) {   System.out.println( 错误!    }   return dayOfweek;   }  //这里添加另一个获取具体星期几的获取方法: public static String getWeek(String time) {   String Week =     SimpleDateFormat format = new SimpleDateFormat( yyyy-MM-dd    Calendar c = Calendar.getInstance();   try {   c.setTime(format.parse(time));   } catch (ParseException e) {   e.printStackTrace();   }   int wek=c.get(Calendar.DAY_OF_WEEK);     if (wek == 1) {   Week +=  星期日    }   if (wek == 2) {   Week +=  星期一    }   if (wek == 3) {   Week +=  星期二    }   if (wek == 4) {   Week +=  星期三    }   if (wek == 5) {   Week +=  星期四    }   if (wek == 6) {   Week +=  星期五    }   if (wek == 7) {   Week +=  星期六    }   return Week;  }

补充一条,获取系统当前时间:

final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

String comment =  Added on   + df.format(new Date());

Log.e( 当前时间为  + comment);

打印出来的格式为:xxxx年xx月xx日 xx:xx:xx

14580.html

cjava