标签:直接 ati hub oid gety void color cal att
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
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
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
说明:
示例工程:https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test29/test1
参考:
https://www.zhihu.com/question/35650798(以上内容转自此篇文章)
标签:直接 ati hub oid gety void color cal att
原文地址:http://www.cnblogs.com/EasonJim/p/7896213.html