java 中获取时间的方法

/*
   * 如何获取系统当前日期
   */
  public static String getNowTime(){
    Calendar ca = Calendar.getInstance();
       int year = ca.get(Calendar.YEAR);//获取年份
       int month=ca.get(Calendar.MONTH);//获取月份
       int day=ca.get(Calendar.DATE);//获取日
       String riqi = String.valueOf(year+"-"+month+"-"+day);
       int minute=ca.get(Calendar.MINUTE);//分
       int hour=ca.get(Calendar.HOUR);//小时
       int second=ca.get(Calendar.SECOND);//秒
       String time = String.valueOf(hour+"-"+minute+"-"+second);
       int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
       String weekDay = "星期"+String.valueOf(WeekOfYear);
       return riqi+"  " + time + "  " + weekDay ;
  }
 
  /**
  * 获得指定日期的前一天
  * @param specifiedDay
  * @return
  * @throws Exception
  */
  public static String getSpecifiedDayBefore(String specifiedDay){
  Calendar c = Calendar.getInstance();
  Date date=null;
  try {
  date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
  } catch (ParseException e) {
  e.printStackTrace();
  }
  c.setTime(date);
  int day=c.get(Calendar.DATE);
  c.set(Calendar.DATE,day-1);

  String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  return dayBefore;
  }
  /**
  * 获得指定日期的后一天
  * @param specifiedDay
  * @return
  */
  public static String getSpecifiedDayAfter(String specifiedDay){
  Calendar c = Calendar.getInstance();
  Date date=null;
  try {
  date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
  } catch (ParseException e) {
  e.printStackTrace();
  }
  c.setTime(date);
  int day=c.get(Calendar.DATE);
  c.set(Calendar.DATE,day+1);

  String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  return dayAfter;
  }
 
  /*
   * 如何获取指定日期的前一天是周几
   */
  public static String getBefforeWeekday(String specifiedDay){
   String []ss = specifiedDay.split("-");
   int y=Integer.parseInt(ss[0]);
      int m=Integer.parseInt(ss[1]);
   int d=Integer.parseInt(ss[2]);
      int n=d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1;
      String weekDay = null ;
      switch((n-1)%7){
    case 1:weekDay = "星期一";
   break;
    case 2:weekDay = "星期二";
   break;
    case 3:weekDay = "星期三";
   break;
    case 4:weekDay = "星期四";
   break;
    case 5:weekDay = "星期五";
   break;
    case 6:weekDay = "星期六";
   break;
    case 7:weekDay = "星期天";
      }
  return weekDay;
  }
 
  /*
   * 如何获取指定日期的下一天是周几
   */
 
  public static String getAffterBefforeWeekday(String specifiedDay){
   String []ss = specifiedDay.split("-");
   int y=Integer.parseInt(ss[0]);
      int m=Integer.parseInt(ss[1]);
   int d=Integer.parseInt(ss[2]);
      int n=d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1;
      String weekDay = null ;
      switch((n+1)%7){
    case 1:weekDay = "星期一";
   break;
    case 2:weekDay = "星期二";
   break;
    case 3:weekDay = "星期三";
   break;
    case 4:weekDay = "星期四";
   break;
    case 5:weekDay = "星期五";
   break;
    case 6:weekDay = "星期六";
   break;
    case 7:weekDay = "星期天";
      }
  return weekDay;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值