package blog.ytso.com.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Number_Of_Days { /** * 获取当月的 天数 */ 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
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/14580.html