Java(Android中) 常用的时间转换关系

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangliniqng/article/details/83743907

欢迎关注技术公众号,微信号搜索ColorfulCode 代码男人

分享技术文章,投稿分享,不限技术种类,不限技术深度,让更多人因为分享而受益。

1.获取当前时间

   long time = System.currentTimeMillis();
   final Date date = new Date(time);
   SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  EEEE");
   这种格式下获取的是 某年某月某日 星期x


   
2.判断某一天是星期几
 

  Calendar time = Calendar.getInstance();
  time.set(year,month,dayOfMonth);
  time.get(Calendar.DAY_OF_WEEK);
  switch(time.get(Calendar.DAY_OF_WEEK))
  {
   case 1:
         "周日"
         break;
     case 2:
         "周一"
          break;                                                       
     case 3:
           "周二"
           break;
     case 4:
           "周三"
           break;                  
       case 5:
           "周四"
            break;
       case 6:
             "周五"
             break;
       case 7:
             "周六"
              break;
         default:
             break;                                                                                      
}

3.

/**
 * 时间转化为时间戳 s级 
 *
 * @param s
 * @return
 * @throws ParseException
 */
public static String dateToStamp(String s) throws ParseException {
    String res;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date date = simpleDateFormat.parse(s);
    long ts = date.getTime() / 1000;
    res = String.valueOf(ts);
    return res;
}

4.

/**
 * 获取指定日期所在周的周六
 * 注意的是  以国外为准 周日是第一天 周六是最后一天
 *
 * @param date 日期
 */
public static Date getLastDayOfWeek(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    // 如果是周6直接返回
    if (c.get(Calendar.DAY_OF_WEEK) == 7) {
        return date;
    }
    c.add(Calendar.DATE, 6 - c.get(Calendar.DAY_OF_WEEK) + 1);
    return c.getTime();
}


/**
 * 根据当前周获取所在周的周日
 *
 * @param date
 * @return
 */
public static Date getLastDayOfWeek7(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    if (c.get(Calendar.DAY_OF_WEEK) == 1) {
        return date;
    }
    c.add(Calendar.DATE, 1 - c.get(Calendar.DAY_OF_WEEK));
    return c.getTime();
}

猜你喜欢

转载自blog.csdn.net/huangliniqng/article/details/83743907