说明:从LocalDate的API上看,主要用于快速获取当前年月日,而DateFormatter也基本上伴随着使用。如果是操作Date对象的,主要是用于时间戳等,伴随着使用的是SimpleDateFormat。
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);
}
}

说明:这里不建议使用全局Calendar,可以直接new一个出来new SimpleDateFormat().getCalendar()

结果如下:

year: 2015
month: 9
day: 17

1.2、joda-time(推荐)

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: 15
monthOfYear: 9
dayOfMonth: 17

2、Java 8,直接使用new datetime api (推荐!!)

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: 2015
Month: 9
DayOfMonth: 17

说明:

  1. Java 8提供的datetime api参考了jodatime,因此两者看起来很相似,使用Java 8 datetime api 时,如果要将日期时间存储到数据库,还应考虑数据库驱动是否支持该api
  2. Java 7及之前版本,推荐使用jodatime,省时省力省心

示例工程:https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test29/test1

参考:

https://www.zhihu.com/question/35650798(以上内容转自此篇文章)

Java获取指定时间(转)的更多相关文章

  1. Java获取指定时间的毫秒值的方法

    有以下两种方法获取指定时间的毫秒值: 1.Calendar类 先由getInstance获取Calendar对象,然后用clear方法将时间重置为(1970.1.1 00:00:00),接下来用set ...

  2. java获取指定时间的年月日

    作者:Night Silent链接:http://www.zhihu.com/question/35650798/answer/63983440来源:知乎著作权归作者所有,转载请联系作者获得授权.1. ...

  3. java学习第13天( java获取当前时间,有关大数据的运算及精确数字运算,Date类)

    一 java获取当前时间 学习一个函数,得到当前时间的准确值 System.currectTimeMillis(). 可以得到以毫秒为单位的当前时间.它主要用于计算程序运行时间,long start= ...

  4. java获取前一天时间SimpleDateFormat,java判断某个时间段

    java获取前一天时间SimpleDateFormat SimpleDateFormat predf = new SimpleDateFormat("yyyy-MM-dd"); D ...

  5. Java获取系统时间少了八个小时

    Java获取系统时间少了八个小时 今天忽然遇到需要获取当前时间的问题,我向来谨慎,先测试获取到的系统时间是否正确,结果竟然发现少了八个小时,晕死了,记得之前在页面用javascript获取过当前时间, ...

  6. java 获取当前时间,前一天时间

    java获取当前时间,并按一定格式输出 1.用Calendar获取Date Calendar calendar=Calendar.getInstance(); SimpleDateFormat for ...

  7. android java获取当前时间的总结

    import   java.text.SimpleDateFormat; SimpleDateFormat   formatter   =   new   SimpleDateFormat   (&q ...

  8. java获取当前日期时间代码总结

    1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值.  方法如下: 要使用 java.util.Date .获取当前时间的代码如下  代码如下 复制代码 Date date = new ...

  9. Java 获取指定日期的方法汇总

    import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; impo ...

随机推荐

  1. The 2018 ACM-ICPC Chinese Collegiate Programming Contest Caesar Cipher

    #include <iostream> #include <cstdio> #include <cstring> #include <string> # ...

  2. OpenSSL SNI

    网站ssl检测工具 https://www.ssllabs.com/ssltest/analyze.html?d=gggl.houseoflux.com.cn https://myssl.com/  ...

  3. 并查集 - BZOJ 1104 [POI2007]洪水

    BZOJ 1104 [POI2007]洪水 描述 AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了他的所有顾问(包括你 ...

  4. MIME类型-服务端验证上传文件的类型

    MIME的作用 : 使客户端软件,区分不同种类的数据,例如web浏览器就是通过MIME类型来判断文件是GIF图片,还是可打印的PostScript文件. web服务器使用MIME来说明发送数据的种类, ...

  5. IOS开发之----全局变量extern的使用

    extern,作用在IOS中,为了使用全局变量.比写在appDelegate和定义单例方便一些: 举例: 1.MyExternClass.h添加这个类,并在.m文件添加 代码 #import &quo ...

  6. dubbo Protocol实现剖析

    title: dubbo Protocol实现剖析 date: 2018-09-09 19:10:07 tags: --- 2.6.3版本,之前读的是2.4.9版本 本篇主要阐述dubbo rpc的c ...

  7. SQL 与或运算

    如果一个字段需要同时包含多个信息点, 最佳的方法是进行位运算,如:1,2,4,8,16 根据与运算进行判断,如一个字段为7,判断2是否存在, 7&2 = 2为ture时,表示存在,反之亦然, ...

  8. VirtualBox 安装XP虚拟机, 安装DB2

    个人随笔记录,也许说的不太清楚. 1. 用google搜索VirtualBox, 找到下载地址,下载,我的是win7,下载64bit的. 2. 下载后,安装VBox软件,这个没遇到问题. 3. 因为我 ...

  9. [automator篇][9] 列表,找孩子

    private boolean ClickByCollInfo(int CLICK, String classname, String id, String text) { UiSelector ui ...

  10. 以前刷过的数位dp

    TOJ1688: Round Numbers Description The cows, as you know, have no fingers or thumbs and thus are una ...