java获取指定时间的年月日
来源:互联网 发布:js获取event对象 编辑:程序博客网 时间:2024/04/30 11:33
1. java 7 及之前版本
1.1 使用 java.util.Calendar (不推荐)
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Demo { public static void main(String[] args) throws ParseException { Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2015-09-17 20:27:00"); Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + 1; // 0-based! int day = now.get(Calendar.DAY_OF_MONTH); System.out.println("year: " + year); System.out.println("month: " + month); System.out.println("day: " + day); }}
结果如下:
year: 2015month: 9day: 17
import org.joda.time.LocalDate;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;public class Demo { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss"); LocalDate localDate = formatter.parseLocalDate("2015-09-17 20:27:00"); System.out.println("yearOfCentury: " + localDate.getYearOfCentury()); System.out.println("monthOfYear: " + localDate.getMonthOfYear()); System.out.println("dayOfMonth: " + localDate.getDayOfMonth()); }}
结果如下:
yearOfCentury: 15monthOfYear: 9dayOfMonth: 17
import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class Demo { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime ldt = LocalDateTime.parse("2015-09-17 20:27:00", formatter); System.out.println("Year: " + ldt.getYear()); System.out.println("Month: " + ldt.getMonth().getValue()); System.out.println("DayOfMonth: " + ldt.getDayOfMonth()); }}
结果如下:
Year: 2015Month: 9DayOfMonth: 17
0 0
- java获取指定时间的年月日
- Java获取当前时间的年月日方法
- Java 获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- PHP 获取指定年月日的开始和结束时间戳
- Java获取当前年月日、时间
- Linux查找占用的端口,并杀死进程
- vi编辑只读文档无法保存的解决办法
- 用c++控制小数的有效位
- Android-25种开源炫酷动画框架
- EF cedefirst数据库迁移
- java获取指定时间的年月日
- foxmail总是提示用户名和密码不对?
- Pure CSS for multiline truncation with ellipsis
- JMeter学习-009-JMeter 后置处理器实例之 - 正则表达式提取器(二)多参数获取
- CentOS7修改网卡名字
- mybatis xml文件中的大于、小于、及like模糊查询的写法
- deep learning学习-caffe安装记录
- SAP 函数的异步调用
- 你会做Web上的用户登录功能吗?