您的位置:首页 > 其它

系统时间、星期、年月日

2017-02-04 16:54 127 查看
获取系统的当前时间

long time = System.currentTimeMillis();


以不同的格式显示

(1) 2015-11-11 11:08:34

(2) 2015/01/06 06-36-23

……

SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Date date = new Date(System.currentTimeMillis());

String str_date = sdf.format(date);


通过Calendar类获取年、月、日、星期(int类型1~7)

Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);

//从0开始,一月得到的month=0
int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DAY_OF_MONTH);

int weekk = calendar.get(Calendar.DAY_OF_WEEK);


获取当前是星期几

Date date = new Date(System.currentTimeMillis());

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

String week = "周";

if(calendar.get(Calendar.DAY_OF_WEEK) == 1)
{
week +="日";
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 2)
{
week +="一";
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 3)
{
week +="二";
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 4)
{
week +="三";
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 5)
{
week +="四";
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 6)
{
week +="五";
}else if(calendar.get(Calendar.DAY_OF_WEEK) == 7)
{
week +="六";
}

//最终获取的格式是:周六
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息