Skip to Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

Converting YMDHMS to a Calendar or Epoch Seconds

Problem

You have year, month, day, hour, minute, and maybe even seconds, and you need to convert it to a Calendar or a Date.

Solution

Use the Calendar class’s set(y,m,d,h,m[,s]) method, which allows you to set the date/time fields to whatever you wish. Note that when using this form and providing your own numbers or when constructing either a Date or a GregorianCalendar object, the month value is zero-based while all the other values are true-origin. Presumably, this is to allow you to print the month name from an array without having to remember to subtract one, but it is confusing.

// GregCalDemo.java 
GregorianCalendar d1 = new GregorianCalendar(1986, 04, 05); // May 5 
GregorianCalendar d2 = new GregorianCalendar(  );    // today 
Calendar d3 = Calendar.getInstance(  );    // today 
 
System.out.println("It was then " + d1.getTime(  )); 
System.out.println("It is now " + d2.getTime(  )); 
System.out.println("It is now " + d3.getTime(  )); 
d3.set(Calendar.YEAR, 1915); 
d3.set(Calendar.MONTH, Calendar.APRIL); 
d3.set(Calendar.DAY_OF_MONTH, 12); 
System.out.println("D3 set to " + d3.getTime(  ));

This prints the dates as shown:

It was then Mon May 05 00:00:00 PDT 1986 
It is now Sun Jul 18 22:51:47 PDT 1999 
It is now Sun Jul 18 22:51:47 PDT 1999 
D3 set to Mon Apr 12 22:51:47 PDT 1915
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java I/O

Java I/O

Elliotte Rusty Harold
Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham

Publisher Resources

ISBN: 0596001703Catalog PageErrata