用java实现日期类的加减
用java实现要求,我完全没概念啊!还请大神帮忙啊!可发我邮箱i2012521@163.com日期类要求:该系统主要处理时间的相关信息。完成以下的操作:日期的设置、显示、...
用java实现要求,我完全没概念啊!还请大神帮忙啊!可发我邮箱i2012521@163.com
日期类
要求:
该系统主要处理时间的相关信息。
完成以下的操作:日期的设置、显示、实现在某一个日期上(月、日、年)加减一个天数的功能、计算两个日期间隔天数
提示:
建立日期类
日期类的初始化
按某个固定格式(如:yyyy-MM-dd)一次性的设置某个日期对象
以固定格式显示某个日期
在日期上加一天
在日期上减一天
输入两个日期,然后输出日期的间隔天数 展开
日期类
要求:
该系统主要处理时间的相关信息。
完成以下的操作:日期的设置、显示、实现在某一个日期上(月、日、年)加减一个天数的功能、计算两个日期间隔天数
提示:
建立日期类
日期类的初始化
按某个固定格式(如:yyyy-MM-dd)一次性的设置某个日期对象
以固定格式显示某个日期
在日期上加一天
在日期上减一天
输入两个日期,然后输出日期的间隔天数 展开
5个回答
展开全部
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
public class CalendarExample {
private static void CalendarTimemethod() {
Date date = Calendar.getInstance().getTime();
System.out.println("Current date and time is: " + date);
System.out.println();
}
private static void SimpleDateFormatmethod() {
Calendar date = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current date and time in simple date format: "
+ dateformatter.format(date.getTime()));
System.out.println();
}
private static void Adddates() {
System.out.println("Performing operations on calendar dates.");
// Get today's date
Calendar date = Calendar.getInstance();
Calendar cldr;
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, -(365 * 2));
System.out.println("Before two years it was: "
+ dateformatter.format(cldr.getTime()));
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, +5);
System.out.println("After five years it will be: "
+ dateformatter.format(cldr.getTime()));
System.out.println();
}
private static void DateDifference() {
System.out.println("Difference between two dates");
Date startDate1 = new GregorianCalendar(2005, 02,
25, 14, 00).getTime();
Date endDate1 = new Date();
;
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println(" Difference between " + endDate1);
System.out.println(" and " + startDate1 + " is " + (diff /
(1000L * 60L * 60L * 24L)) + " days.");
System.out.println();
}
private static void Getcalendermethods() {
System.out.println("Various get methods of the calendar class:");
Calendar calender = Calendar.getInstance();
System.out.println("Year : "
+ calender.get(Calendar.YEAR));
System.out.println("Month : "
+ calender.get(Calendar.MONTH));
System.out.println("Day of Month : "
+ calender.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of Week : "
+ calender.get(Calendar.DAY_OF_WEEK));
System.out.println("Day of Year : "
+ calender.get(Calendar.DAY_OF_YEAR));
System.out.println("Week of Year : "
+ calender.get(Calendar.WEEK_OF_YEAR));
System.out.println("Week of Month : "
+ calender.get(Calendar.WEEK_OF_MONTH));
System.out.println("Day of the Week in Month : "
+ calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : " + calender.get(Calendar.HOUR));
System.out.println("AM PM : " + calender.get(Calendar.AM_PM));
System.out.println("Hour of the Day : "
+ calender.get(Calendar.HOUR_OF_DAY));
System.out.println("Minute : " + calender.get(Calendar.MINUTE));
System.out.println("Second : " + calender.get(Calendar.SECOND));
System.out.println();
}
public static void main(String[] args) {
System.out.println();
CalendarTimemethod();
SimpleDateFormatmethod();
Adddates();
DateDifference();
Getcalendermethods();
}
}
以上答案有林氏120烫伤中心提供。稍微再修改下就可以
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
public class CalendarExample {
private static void CalendarTimemethod() {
Date date = Calendar.getInstance().getTime();
System.out.println("Current date and time is: " + date);
System.out.println();
}
private static void SimpleDateFormatmethod() {
Calendar date = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current date and time in simple date format: "
+ dateformatter.format(date.getTime()));
System.out.println();
}
private static void Adddates() {
System.out.println("Performing operations on calendar dates.");
// Get today's date
Calendar date = Calendar.getInstance();
Calendar cldr;
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, -(365 * 2));
System.out.println("Before two years it was: "
+ dateformatter.format(cldr.getTime()));
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, +5);
System.out.println("After five years it will be: "
+ dateformatter.format(cldr.getTime()));
System.out.println();
}
private static void DateDifference() {
System.out.println("Difference between two dates");
Date startDate1 = new GregorianCalendar(2005, 02,
25, 14, 00).getTime();
Date endDate1 = new Date();
;
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println(" Difference between " + endDate1);
System.out.println(" and " + startDate1 + " is " + (diff /
(1000L * 60L * 60L * 24L)) + " days.");
System.out.println();
}
private static void Getcalendermethods() {
System.out.println("Various get methods of the calendar class:");
Calendar calender = Calendar.getInstance();
System.out.println("Year : "
+ calender.get(Calendar.YEAR));
System.out.println("Month : "
+ calender.get(Calendar.MONTH));
System.out.println("Day of Month : "
+ calender.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of Week : "
+ calender.get(Calendar.DAY_OF_WEEK));
System.out.println("Day of Year : "
+ calender.get(Calendar.DAY_OF_YEAR));
System.out.println("Week of Year : "
+ calender.get(Calendar.WEEK_OF_YEAR));
System.out.println("Week of Month : "
+ calender.get(Calendar.WEEK_OF_MONTH));
System.out.println("Day of the Week in Month : "
+ calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : " + calender.get(Calendar.HOUR));
System.out.println("AM PM : " + calender.get(Calendar.AM_PM));
System.out.println("Hour of the Day : "
+ calender.get(Calendar.HOUR_OF_DAY));
System.out.println("Minute : " + calender.get(Calendar.MINUTE));
System.out.println("Second : " + calender.get(Calendar.SECOND));
System.out.println();
}
public static void main(String[] args) {
System.out.println();
CalendarTimemethod();
SimpleDateFormatmethod();
Adddates();
DateDifference();
Getcalendermethods();
}
}
以上答案有林氏120烫伤中心提供。稍微再修改下就可以
展开全部
Java代码
public class test1 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;
int date=c.get(Calendar.DATE);
System.out.println("今天是"+year+"年"+month+"月"+date+"日");
System.out.println("是今年的第"+c.get(Calendar.DAY_OF_YEAR)+"天");
System.out.println("c.getTime()的结果: "+c.getTime());
System.out.println("new Date()的结果: "+new Date());
c.set(Calendar.DAY_OF_YEAR, date + 30);
System.out.println("17天后是"+c.getTime());
}
}
Java代码
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 Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
注意int month=c.get(Calendar.MONTH)+1哦,好像系统是从0开始计月份,到了12月就归零了。所以单独取月份时,要在后面加一才能得到当前的月份。
calender日期加减后赋值给Date类型
Java代码
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time=sdf.format(new Date());
Calendar cd = Calendar.getInstance();
try {
cd.setTime(sdf.parse(time));
} catch (ParseException e) {
e.printStackTrace();
}
cd.add(Calendar.DATE, 1);//增加一天
//cal.add(Calendar.DATE, -1); //减一天
//cd.add(Calendar.MONTH, 1);//增加一月
Date date=cd.getTime();
System.out.println(sdf.format(date));
将yyyy//MM/dd的字符串类型转为Date类型
Java代码
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
str12 = format.parse(str12_1);
在excel导入数据时,日期类型的数据直接获取
Java代码
CellType t1 = st.getCell(11, row).getType();
Date regDate = null;
Date str12=null;//出生年月,不能为空
if (t1 == CellType.DATE)
{
DateCell regCell = (DateCell) st.getCell(11, row);
str12 = regCell.getDate();
}
public class test1 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;
int date=c.get(Calendar.DATE);
System.out.println("今天是"+year+"年"+month+"月"+date+"日");
System.out.println("是今年的第"+c.get(Calendar.DAY_OF_YEAR)+"天");
System.out.println("c.getTime()的结果: "+c.getTime());
System.out.println("new Date()的结果: "+new Date());
c.set(Calendar.DAY_OF_YEAR, date + 30);
System.out.println("17天后是"+c.getTime());
}
}
Java代码
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 Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
注意int month=c.get(Calendar.MONTH)+1哦,好像系统是从0开始计月份,到了12月就归零了。所以单独取月份时,要在后面加一才能得到当前的月份。
calender日期加减后赋值给Date类型
Java代码
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time=sdf.format(new Date());
Calendar cd = Calendar.getInstance();
try {
cd.setTime(sdf.parse(time));
} catch (ParseException e) {
e.printStackTrace();
}
cd.add(Calendar.DATE, 1);//增加一天
//cal.add(Calendar.DATE, -1); //减一天
//cd.add(Calendar.MONTH, 1);//增加一月
Date date=cd.getTime();
System.out.println(sdf.format(date));
将yyyy//MM/dd的字符串类型转为Date类型
Java代码
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
str12 = format.parse(str12_1);
在excel导入数据时,日期类型的数据直接获取
Java代码
CellType t1 = st.getCell(11, row).getType();
Date regDate = null;
Date str12=null;//出生年月,不能为空
if (t1 == CellType.DATE)
{
DateCell regCell = (DateCell) st.getCell(11, row);
str12 = regCell.getDate();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用java.util.Calendar.
Calendar.getInstance()获得Calendar对象
public abstract void add(int field,
int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点:
add(Calendar.DAY_OF_MONTH, -5)。
参数:field - 日历字段。
amount - 为字段添加的日期或时间量。
间隔天数用getTimeInMillis()的值相减,再把毫秒换算成天就可以了
Calendar.getInstance()获得Calendar对象
public abstract void add(int field,
int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点:
add(Calendar.DAY_OF_MONTH, -5)。
参数:field - 日历字段。
amount - 为字段添加的日期或时间量。
间隔天数用getTimeInMillis()的值相减,再把毫秒换算成天就可以了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
获取时间后 再getTime();
获得时间戳值,就可以进行加减了,
加减后再转换回来就好了。
获得时间戳值,就可以进行加减了,
加减后再转换回来就好了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-12-25
展开全部
请查阅
java.util.Calendar
里面有你说的这些功能所需的基本函数。
java.util.Calendar
里面有你说的这些功能所需的基本函数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询