用java程序写 使用日期类计算当前日期的前50天是哪一天,是星期几
答案:4 悬赏:50 手机版
解决时间 2021-03-25 22:22
- 提问者网友:我一贱你就笑
- 2021-03-25 13:57
用java程序写 使用日期类计算当前日期的前50天是哪一天,是星期几
最佳答案
- 五星知识达人网友:罪歌
- 2021-03-25 14:05
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static Date dateAdd(int days) {
// 日期处理模块 (将日期加上某些天或减去天数)返回字符串
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
return canlendar.getTime();
}
public static void main(String[] args) {
Date date = Test.dateAdd(-50);
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("50天前日期为:" + sdfd.format(date));
SimpleDateFormat sdf = new SimpleDateFormat("E");
System.out.println("为:" + sdf.format(date));
}
}使用Calendar 和 SimpleDateFormat两个日期类可搞定!有问题再追问,good luck!
import java.util.Calendar;
import java.util.Date;
public class Test {
public static Date dateAdd(int days) {
// 日期处理模块 (将日期加上某些天或减去天数)返回字符串
Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
return canlendar.getTime();
}
public static void main(String[] args) {
Date date = Test.dateAdd(-50);
SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("50天前日期为:" + sdfd.format(date));
SimpleDateFormat sdf = new SimpleDateFormat("E");
System.out.println("为:" + sdf.format(date));
}
}使用Calendar 和 SimpleDateFormat两个日期类可搞定!有问题再追问,good luck!
全部回答
- 1楼网友:一把行者刀
- 2021-03-25 16:42
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, -50);
System.out.println(c.getTime());
或者
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -50);
System.out.println(c.getTime());
c.add(Calendar.DAY_OF_YEAR, -50);
System.out.println(c.getTime());
或者
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -50);
System.out.println(c.getTime());
- 2楼网友:上分大魔王
- 2021-03-25 16:04
public static void main(String[] args) {
Date date = new Date(); // 新建一个日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 格式化日期
String beforeDate = sdf.format(getDateBefore(date, 50));
System.out.println(beforeDate);
String afterDate = getWeekOfDate(sdf.format(getDateAfter(date, 50)));
System.out.println(afterDate);
}
public static Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}
当前日期是星期几
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
Date date = new Date(); // 新建一个日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 格式化日期
String beforeDate = sdf.format(getDateBefore(date, 50));
System.out.println(beforeDate);
String afterDate = getWeekOfDate(sdf.format(getDateAfter(date, 50)));
System.out.println(afterDate);
}
public static Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}
当前日期是星期几
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
- 3楼网友:走死在岁月里
- 2021-03-25 15:18
Calendar ca = Calendar.getInstance();
Date now = ca.getTime();
ca.add(Calendar.DAY_OF_MONTH, -50);
Date fu = ca.getTime();
System.out.println("当前日期"+now);
System.out.println("50天以前日期"+fu);
Date now = ca.getTime();
ca.add(Calendar.DAY_OF_MONTH, -50);
Date fu = ca.getTime();
System.out.println("当前日期"+now);
System.out.println("50天以前日期"+fu);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯