1 package huolongluo.family.util; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 7 /** 8 * Created by 火龙裸 on 2018/7/13. 9 */ 10 11 public class Number_Of_Days 12 { 13 /** 14 * 获取当月的 天数 15 */ 16 public static int getCurrentMonthDay() 17 { 18 Calendar a = Calendar.getInstance(); 19 a.set(Calendar.DATE, 1); 20 a.roll(Calendar.DATE, -1); 21 int maxDate = a.get(Calendar.DATE); 22 return maxDate; 23 } 24 25 /** 26 * 根据 年、月 获取对应的月份 的 天数 27 */ 28 public static int getDaysByYearMonth(int year, int month) 29 { 30 Calendar a = Calendar.getInstance(); 31 a.set(Calendar.YEAR, year); 32 a.set(Calendar.MONTH, month - 1); 33 a.set(Calendar.DATE, 1); 34 a.roll(Calendar.DATE, -1); 35 int maxDate = a.get(Calendar.DATE); 36 return maxDate; 37 } 38 39 /** 40 * 根据日期 找到对应日期的 星期几 41 * 42 * @param date 比如传参:2018-07-13 将返回“周五” 43 */ 44 public static String getDayOfWeekByDate(String date) 45 { 46 String dayOfweek = "-1"; 47 try 48 { 49 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); 50 Date myDate = myFormatter.parse(date); 51 SimpleDateFormat formatter = new SimpleDateFormat("E"); 52 String str = formatter.format(myDate); 53 dayOfweek = str; 54 } 55 catch (Exception e) 56 { 57 System.out.println("错误!"); 58 } 59 return dayOfweek; 60 } 61 }
这里添加另一个获取具体星期几的获取方法:
/** * 根据当前日期获得是星期几 * time=yyyy-MM-dd * @return */ 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