Java Date and Calendar examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar.

1. Java Date Examples
Few examples to work with Date APIs.

Example 1.1 – Convert Date to String.

SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
String date = sdf.format(new Date());
System.out.println(date); //15/10/2013

Example 1.2 – Convert String to Date.

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "31-08-1982 10:20:56";
Date date = sdf.parse(dateInString);
System.out.println(date); //Tue Aug 31 10:20:56 SGT 1982

P.S Refer to this – SimpleDateFormat JavaDoc for detail date and time patterns.

Example 1.3 – Get current date time

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2013/10/15 16:16:39

Example 1.4 – Convert Calendar to Date

Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
2. Java Calendar Examples
Few examples to work with Calendar APIs.

Example 2.1 – Get current date time

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
Calendar calendar = new GregorianCalendar(2013,0,31);
System.out.println(sdf.format(calendar.getTime()));

Output

2013 Jan 31 00:00:00

Example 2.2 – Simple Calendar example

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
Calendar calendar = new GregorianCalendar(2013,1,28,13,24,56);

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

int hour = calendar.get(Calendar.HOUR); // 12 hour clock
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);

System.out.println(sdf.format(calendar.getTime()));

System.out.println("year \t\t: " + year);
System.out.println("month \t\t: " + month);
System.out.println("dayOfMonth \t: " + dayOfMonth);
System.out.println("dayOfWeek \t: " + dayOfWeek);
System.out.println("weekOfYear \t: " + weekOfYear);
System.out.println("weekOfMonth \t: " + weekOfMonth);

System.out.println("hour \t\t: " + hour);
System.out.println("hourOfDay \t: " + hourOfDay);
System.out.println("minute \t\t: " + minute);
System.out.println("second \t\t: " + second);
System.out.println("millisecond \t: " + millisecond);

Output

2013 Feb 28 13:24:56
year : 2013
month : 1
dayOfMonth : 28
dayOfWeek : 5
weekOfYear : 9
weekOfMonth : 5
hour : 1
hourOfDay : 13
minute : 24
second : 56
millisecond : 0

Example 2.3 – Set a date manually.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");

Calendar calendar = new GregorianCalendar(2013,1,28,13,24,56);
System.out.println("#1. " + sdf.format(calendar.getTime()));

//update a date
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.MINUTE, 33);

System.out.println("#2. " + sdf.format(calendar.getTime()));

Output

#1. 2013 Feb 28 13:24:56
#2. 2014 Dec 28 13:33:56

Example 2.4– Add or subtract from a date.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");

Calendar calendar = new GregorianCalendar(2013,10,28);
System.out.println("Date : " + sdf.format(calendar.getTime()));

//add one month
calendar.add(Calendar.MONTH, 1);
System.out.println("Date : " + sdf.format(calendar.getTime()));

//subtract 10 days
calendar.add(Calendar.DAY_OF_MONTH, -10);
System.out.println("Date : " + sdf.format(calendar.getTime()));

Output

Date : 2013 Nov 28
Date : 2013 Dec 28
Date : 2013 Dec 18

Example 2.5– Convert Date to Calendar.

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "22-01-2015 10:20:56";
Date date = sdf.parse(dateInString);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

https://www.mkyong.com/java/java-date-and-calendar-examples/
http://www.mkyong.com/tutorials/java-date-time-tutorials/

原文地址:https://www.cnblogs.com/shy1766IT/p/10105170.html

时间: 2024-11-03 23:06:20

Java Date and Calendar examples的相关文章

java Date和Calendar类

最近在无聊的看书,遇到一编程题目,问题描述如下: 黑色星期五源于西方迷信:耶稣基督死于星期五,而13也是一个不吉利的数字.黑色星期五即该日同时是星期五又是13号,求未来几年内这样的日子. 基于该问题会涉及到java中的关于时间类的部分,故尝试通过该题目总结现阶段的java Date和calendar类的问题. 一.Date类 查阅API文档可知,Date类源于jdk1.0版本,并在jdk1.1版本中其绝大多数方法被Calendar类中方法所代替.Date类构造函数public且未abstrct,

Java Date, Calendar and Time API - Tutorial

Java Date and Time API This article explains the API for using Calendar, Date and Time in Java and how to format the output of a date. Table of Contents 1. Overview 2. Format date 3. Working with Dates and Calendars 3.1. Calendar 3.2. Date and Date C

Java日期与时间的处理/Date,String,Calendar转换

public class Demo01 { //Java中Date类和Calendar简介 public static void main(String[] args) { long now=System.currentTimeMillis(); System.out.println("now= "+now); Date d1=new Date(now); System.out.println("d1= "+d1); Calendar c1=Calendar.get

java 日期操作,Date、Calendar 操作

java开发中避免不了日期相关操作,这里总结了一些常用方法~ 直接上码: package jse; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; /**  * 常用日期操

Java:日期类Date与Calendar

怎么获取系统系统时间,通过java? Date now= new Date(); SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSSSS"); String nowFormate= dateFormat.format(now); System.out.println(nowFormate); 输出结果 2017-02-16 20:56:12.000000278 如何获取当前系统是星期

Java Date类和Calendar类的一个控制台打印日期的小程序

Java Date类和Calendar类的一个打印日期的小程序,可以直接用. package com.boy.Idate.calendar; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 控制台可视化日历 * @author 田硕 */ public class VisualCalendar {

java===Date,DateFormat,SimpleDateFormat,Calendar类

package 常用类.Date; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /**月份从0-11的整数表示,0是一月,11是12月 * 日期对象-->毫秒值 getTime方法: * 原因:可以通过毫秒值来进行日期运算 * 毫秒值-->日期对象 setTime方法或者构造方法new Date(毫秒

java中Date与Calendar详解

在开发中,Date与Calendar使用率上是相当高的,如果对它们不太了解,那么,在实际开发中会发生各种状态.如:为什么我获得到的本月的月份不是本月等等. Date篇 public class Dateextends Objectimplements Serializable, Cloneable, Comparable<Date>类 Date 表示特定的瞬间,精确到毫秒 在类 Date 所有可以接受或返回年.月.日期.小时.分钟和秒值的方法中,将使用下面的表示形式: 年份 y 由整数 y -

Java中的Date和Calendar的常用用法

在java中用到的最多的时间类莫过于 java.util.Date了,由于Date类中将getYear(),getMonth()等获取年.月.日的方法都废弃了,所以要借助于Calendar来获取年.月.日.周等比较常用的日期格式 注意:以下代码均已在jdk1.6中测试通过,其他版本可能使用不同,请注意! Date与String的相互转换 /**  * Date与String的互转用法,这里需要用到SimpleDateFormat */Date currentTime = new Date();