Java Date,Time With Data type and Epoch

Anojaa Gnaneswaran
4 min readMar 1, 2022

Date ,Time and TimeZone are most important to understand deeply when we developing Applications.The Reason is Time and Date differ from country to country, As they written and read it in different ways.there are a various technologies, languages, and frameworks that we can use to implement localization in software development. In this article, we are going to discuss Date ,Time and TimeZone are handle in Java based Application.

Ancient History of Time

Before we moving to Java based application we Recap ancient history of timekeeping.Religion ,philosophy and science should contain different explanation of Time.Now we have clock for track minute,seconds and hours, But ancient time no this technology.”how they measure Time?”. They measure It by observing nature: the sunrise and sunset ,the moon waning and waxing.

Egyptians divided their sundials into twelve parts from each hour of the day from dusk to down.Although sundials were accurate for their time,they had one evident disadvantage.It practically useless during overcast days and at night.so twelve hours for day time and twelve hour of the night time.that is 24hours Day.

Then we have question rise in our mind “why then each of hour divided into 60 minutes?”. The sexagesimal system takes back to ancient Mesopotamia.It is numerical system based on the number 60 that was developed by the sumerian and later adopted by the baby_lonian.

They developed it by dividing parts of their fingers into single units and units of twelve.It works like this way ,look at the palm of your hand and you see your each of fingers are divided into three sections on one hand.Using their thumbs to count ,then it counted up to twelve (3*4=12). The fifth finger on the other hand each represented a dozen so 12*5= 60.

Finger Calculation

Likewise other countries also used different method use of timekeeping in ancient history.so, in 1967 the National Institute of Standards and Technology(NIST) declared the official measurement of a second is equivalent to 9,192,631,770 oscillations of a Cesium atom’s resonant frequency.

September 1752 calendar missing 11 days

you can click above link and read the story behind the missing 11days in calendar.

11Day missing calender

Greenwich Mean Time (GWT)

Greenwich Mean Time is the solor time at Royal observatory in Greenwich London and It counted from Midnight.British mariners kept at least one chronometer on GMT to calculate their longitude from the greenwich,It consider has zero degrees.You want deep understand about GWT look at below youtube link video .

Coordinated Universal Time (UTC)

Coordinated Universal Time is the generally time standard by which the world regulates clocks and time. It is within about 1 second of mean solar time at zero degrees longitude.UTC is not adjusted for daylight saving time.current number of leap seconds section for the number of leap seconds inserted to date.

Daylight Saving Time (DST)

Daylight Saving Time is called as many names like summer time,daylight time etc, It is practice of advancing clock by one hour forwarding from standard during in the summer times.countries have different change dates.

In The daylight saving idea first come up with benjamin franklin. The idea was first advocated by william willett in the pamphlet.The energy saving benefits of summer time were recognized during world war two.

Sri Lanka, India, China and Japan are the countries that do not consider of daylight saving.

Java.sql.date

The java.sql.Date class represents the only date in Java.The java.sql.Date instance is mostly used in the JDBC because it represents the date that can be stored in a database.Some constructors and methods of java.sql.Date class has been deprecated.

Lets see The sample code snippet of java.sql.date

Data type

The Date in Java is not only a data type, like int or float, but a class. This means it has its own methods available for use.

A Date in Java also includes the time, the year, the name of the day of the week, and the time zone. One of its methods includes extracting the time from the Date object.

Note: In order for this to work, you need to make sure to bring in the Java Date package. At the start of the program, use the import command to do this code

Epoch Time

Epoch Time basically a way that some systems are use to to able to count time.epoch is number of second that gone since 1970 January 01(UTC/GWT),It not counting leap second.In per one day we have 86400second(60*60*24).

Java 8, Date and Time APIs also support getting the epoch time with a deal of date, time and time zone.the below code snippet to get Epoch time for the current time. It is easy to get the current time in Epoch time in java.

long epoch = Instant.now().toEpochMilli();
System.out.println(epoch);

LocalDate ld = Instant.ofEpochMilli(epoch)
.atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(ld);

LocalDateTime ldt = Instant.ofEpochMilli(epoch)
.atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(ldt);

I hope you enjoy with this Article,we meet you all with my next article .

Happy Coding 😊 !

Reference

  1. https://www.javatpoint.com/java-sql-date

2.https://lifeinsaudiarabia.net/why-are-11-days-missing-in-calendar-of/

--

--

Anojaa Gnaneswaran

Faculty of IT, University of Moratuwa, Associated Software Engineer at Virtusa