Date
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);//打印的时间看不懂,希望有些格式
//将模式封装到SimpleDateformat对象中
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E hh:mm:ss");
//调用format方法让模式格式化指定Date对象
String time = sdf.format(d);
System.out.println("time="+time);
}
}
//输出
time=2019年09月14日星期六 06:53:24
Calendar
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
sop(c);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
java.util.GregorianCalendar[time=1568458929722,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=29,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=8,WEEK_OF_YEAR=37,WEEK_OF_MONTH=2,DAY_OF_MONTH=14,DAY_OF_YEAR=257,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=2,SECOND=9,MILLISECOND=722,ZONE_OFFSET=28800000,DST_OFFSET=0]
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
sop(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DAY_OF_MONTH)+"日");
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2019年9月14日
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
sop(c.get(Calendar.YEAR)+"年");
sop(c.get(Calendar.MONTH)+1+"月");
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop("星期"+(c.get(Calendar.DAY_OF_WEEK)-1));
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2019年
9月
14日
星期6
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
sop(c.get(Calendar.YEAR)+"年");
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2019年
九月
14日
星期六
import java.util.Calendar;
public class CalendarDemo0 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(2012,2,23);
printCalendar(c);
}
public static void printCalendar(Calendar c)
{
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
sop(c.get(Calendar.YEAR)+"年");
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2012年
三月
23日
星期五
import java.util.Calendar;
public class CalendarDemo0 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH,-1);
printCalendar(c);
}
public static void printCalendar(Calendar c)
{
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
sop(c.get(Calendar.YEAR)+"年");
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2019年
八月
14日
星期三
获取任意年的二月有多少天。
思路:根据指定年设置一个时间就是
c.set(year,2,1);//某一年的3月1日。
c.add(Calendar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是二月最后一天。
import java.util.Calendar;
public class CalendarDemo0 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(2012,2,1);
c.add(Calendar.DAY_OF_MONTH,-1);
printCalendar(c);
}
public static void printCalendar(Calendar c)
{
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
sop(c.get(Calendar.YEAR)+"年");
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2012年
二月
29日
星期三
获取昨天的现在这个时刻。
c.add(Calendar.DAY_OF_MONTH,-1);
import java.util.Calendar;
public class CalendarDemo0 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH,-1);
printCalendar(c);
}
public static void printCalendar(Calendar c)
{
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
sop(c.get(Calendar.YEAR)+"年");
int index = c.get(Calendar.MONTH);
int index1 = c.get(Calendar.DAY_OF_WEEK);
sop(mons[index]);
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
sop(weeks[index1]);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
2019年
九月
13日
星期五
网友评论