Java Date and Calendar examples
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/
Java Date and Calendar examples的更多相关文章
- Java:Date、Calendar、Timestamp的使用
一.Java.util.Date 该对象包含了年月日时分秒信息.具体使用如下代码: //String 转换为Date private static void dateDemo() throws Par ...
- Java Date 和 Calendar
Java 语言的Date(日期),Calendar(日历),DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分.日期是商业逻辑计算一个关键的部分,所有的开发者都应该能够计算 ...
- Java 时间类-Calendar、Date、LocalDate/LocalTime
1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...
- Java日期与时间的处理/Date,String,Calendar转换
public class Demo01 { //Java中Date类和Calendar简介 public static void main(String[] args) { long now=Syst ...
- java日期类型转换总结date timestamp calendar string
用Timestamp来记录日期时间还是很方便的,但有时候显示的时候是不需要小数位后面的毫秒的,这样就需要在转换为String时重新定义格式. Timestamp转化为String: S ...
- Java:Date、Calendar、Timestamp的区别、相互转换与使用【转载】
1 Java.util.Date 包含年.月.日.时.分.秒信息 包含年.月.日信息. 继承自java.util.Date.在数据库相关操作中使用,如rs.getDate,ps.setDate等.rs ...
- java日期处理 calendar 和date
抄的人家的,仅作学习使用. java Date获取 年月日时分秒 package com.util; import java.text.DateFormat; import java.ut ...
- Java:日期类Date与Calendar
Timestamp类型与日期类型之间的转化? Timestamp timestamp = Timestamp.valueOf("2017-03-17 07:00:00"); Dat ...
- JAVA之旅(二十三)——System,RunTime,Date,Calendar,Math的数学运算
JAVA之旅(二十三)--System,RunTime,Date,Calendar,Math的数学运算 map实在是太难写了,整理得我都晕都转向了,以后看来需要开一个专题来讲这个了,现在我们来时来学习 ...
随机推荐
- Getting in Line UVA 216
Getting in Line Computer networking requires that the computers in the network be linked. This pro ...
- 安装 LUA
为了以后方便,记录一下步骤 1.yum install readline-devel 2.wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 3.cd lua-5 ...
- 算法笔记_231:网格中移动字母(Java)
目录 1 问题描述 2 解决方案 1 问题描述 2x3=6个方格中放入ABCDE五个字母,右下角的那个格空着.如图[1.jpg]所示. 和空格子相邻的格子中的字母可以移动到空格中,比如,图中的C和 ...
- python获取系统开机时间
import psutil import time time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time()))
- JAVA排序总结
package com.softeem.jbs.lesson4; import java.util.Random; /** * 排序测试类 * * 排序算法的分类如下: * 1.插入排序(直接插入排序 ...
- Webwork【08】结合实战简析Controller 配置
虽然现在 MVC 框架层出不穷,但做为 Struts 前身的 webwork. 其经典程度不亚于贝利之于足球,双 11 之于淘宝特卖. 本篇将结合 webwork controller 配置文件 xw ...
- SpringMVC学习笔记一:基本概念,执行流程与开发步骤
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831328.html 一:基本概念 DispatcherServlet:前端控制器,负责把请求分发处理. H ...
- Centos 7 文件和目录管理
查看权限在终端输入: ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些: -rw-rw-r-- 其中: 最前面那个 - 代表的是类型 中 ...
- .NET Core 全新认识
来源:yubinfeng cnblogs.com/yubinfeng/p/6626694.html 一.概述 .NET 经历14年,在Windows平台上的表现已经相当优秀,但是“跨平台.开 ...
- 《Android源代码设计模式解析与实战》读书笔记(十)
第十章.解释器模式 解释器模式是一种用的比較少的行为型模式.其提供了一种解释语言的语法或表达式的方式. 可是它的使用场景确实非常广泛,仅仅是由于我们自己非常少回去构造一个语言的文法,所以使用较少. 1 ...