利用Calendar获取Date中的年、月、日、时、分、秒等
来源:互联网 发布:xampp 数据库密码 编辑:程序博客网 时间:2024/05/16 19:09
java.util.Date中提供的getYear()、getMonth()、getDay()等建议不适用。
这里我们使用java.util.Calendar来替代完成,并且该类还提供了更多实用的扩展功能。
package org.liw.date;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import com.ibm.icu.text.SimpleDateFormat;
/**
* Calendar、Date、SimpleDateFormat
* @author lee.will
*
*/
public class TestCalendar {
/**
* 日期格式
*/
public static final String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss";
/**
* 测试
* @param args
*/
public static void main(String[] args) {
//将日期字符串转换为日期
Date dateTime = null;
String dateTimeStr = "2011.08.18 14:20:35";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
try {
dateTime = simpleDateFormat.parse(dateTimeStr);
} catch (ParseException e) {
System.err.println("日期字符串 转换到 日期出错.");
System.exit(1);
}
//日期转换为日历并获取参数
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);//Calendar.HOUR为12小时制
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
String weekDayStr = turnWeekDaytoUpperCase(weekDay);
//组织数据打印输出
StringBuffer info = new StringBuffer();
info.append(year).append("年").append(month).append("月").append(day).append("日")
.append("\t")
.append(hour).append(":").append(minute).append(":").append(second)
.append("\t")
.append("星期").append(weekDayStr);
System.out.println(info);
}
/**
* 转换日期小写为大写
* @param weekDay
* @return
*/
private static String turnWeekDaytoUpperCase(int weekDay) {
String weekDayStr = null;
switch (weekDay) {
case 1:
weekDayStr = "一";
break;
case 2:
weekDayStr = "二";
break;
case 3:
weekDayStr = "三";
break;
case 4:
weekDayStr = "四";
break;
case 5:
weekDayStr = "五";
break;
case 6:
weekDayStr = "六";
break;
default:
weekDayStr = "日";
break;
}
return weekDayStr;
}
}
- 利用Calendar获取Date中的年、月、日、时、分、秒等
- 利用Calendar获取Date中的年、月、日、时、分、秒等
- Calendar获取年、月、日、时、分、秒、毫秒
- 获取年、月、日、时、分、秒
- 获取与更改Date的年、月、日、时、分、秒
- 如何获取年、月、日、时、分、秒、星期几等
- iOS 获取指定日期的时间(年、月、日、星期、时、分、秒等)
- 单独获取当前的年,月,日,时,分,秒
- C#之获取年、月、日、时、分、秒...
- js获取年、月、日、时、分、秒、毫秒函数
- javascript获取当前时间(年-月-日 时:分:秒)
- js 获取时间:年 月 日 时 分 秒
- 获取当前时间的年、月、日、时、分、秒
- 获取当前时间的年、月、日、时、分、秒
- datetimepicker 年 月 日 时 分 秒
- 年,月,日,时,分,秒
- 当前年、月、日、时、分、秒的获取
- 年/月/日 时:分:秒 --> 年月日时分秒
- android的文件操作
- 属性
- 弹窗输入内容,返回内容 不使用屏幕
- 解决360\小红伞杀毒报网页HTML.Rce.Gen3恶意程序的问题
- Mysql 5.1 开启慢查询日志功能
- 利用Calendar获取Date中的年、月、日、时、分、秒等
- 操作系统内存管理机制
- Java上路02-Hello World
- Freemarker的模板
- 关于PHP的配置问题
- Creating a Game with CocosBuilder
- .net常用语句大全
- 杭电ACM 2001 计算两点间的距离
- struts2中action跳转后页面的css或是js失效的问题