Postingan

Menampilkan postingan dengan label date and time tutorial

17 Examples of Calendar and Date in Java

The java.util.Calendar class was added in Java on JDK 1.4 in an attempt to fix some flaws of the java.util.Date class. It did make some task simpler, e.g. create an arbitrary date comes easier using new GregorianCalendar(2016, Calendar.JUNE, 11) constructor, as opposed to Date class where the year starts from 1900 and Month was starting from zero. It didn't solve all the problems e.g. mutability and thread-safety of Date class still remains, but it does make life easier at that time. Now with Java 8 everything related to Date and Time has become super easy and consistent but unfortunately, it will take another 5 to 10 years before older version of Java goes away. Don't believe me, there are still applications running on JDK 1.5 which was released 12 years ago. The bottom line is it's still important to know about Date and Calendar in Java. Read more �

How to create LocalDateTime in Java 8 - Example

The LocalDateTime is a new class introduced in Java 8 new Date and Time API. This class is in java.time package and it represents both date and time information without timezone. In this example, you will learn different ways to create an instance of LocalDateTime class in Java 8 e.g. by using the static factory method, or by combining LocalDate and LocalTime instances together, which are subsequently used to denote date without time and time without the date in Java 8. As their name suggests they are local, so they don't contain timezone information. They are always bound to local timezone i.e. the timezone of the machine on which your Java program is running. The class which contains the date, time and timezone information is known as ZonedDateTime in Java 8. Read more �

How to convert java.util.Date to java.sql.Timestamp?

You can convert a java.util.Date to java.sql.Timestamp value by using the getTime() method of Date class. This method return the long millisecond value from Epoch (1st January 1970 midnight) which you can pass to java.sql.Timestamp to create a new instance of Timestamp object in JDBC. Remember, java.sql.TimeStamp class is a wrapper around java.util.Date to allow JDBC to view it as SQL TIMESTAMP value. Only way to create a Timestamp instance is by passing the long time value because the second constructor of Timestamp class, which accepts individual fields e.g. year, month, date, hour, minute, second and nano is deprecated. Timestamp class can also hold up-to nano second value. Read more �

How to convert java.util.Date to java.time.LocalDate in Java 8 - Example

The easiest way to convert a java.util.Date to java.time.LocalDate is via Instant, which is the equivalent class of java.util.Date in JDK 8. You can first convert util Date to Instant and then create a LocalDateTime object from that instant at your default timezone. Once you have an instant of LocalDateTime, you can easily convert that to LocalDate or LocalTime in Java. If you are not living under the rock in last two years that its 2016 and Java 8 has been around for more than 2 years, 18th March 2014 Java SE 8 GA happened. In the third attempt of designing a robust date and time API, JDK 8 has come up with JSR 318 new Date and Time API and many programmers have already started using it. Read more �

3 ways to get number of months and year between two dates in Java?

Earlier I have talked about how to calculate a number of days between two dates in Java ( see here ), and now you will learn how to get the number of months and years between dates . Though it may look easy, it's not that easy to calculate months and years between dates, unless you take care of all kinds of nuisances like Daylight saving time and when daylight change occurs, leap seconds, and an extra day added in a leap year. Unfortunately, JDK doesn't have a standard method like Period.between() method of Java 8 before to solve this problem. So, if you have to calculate the number of months and years between given dates, you have three main options, first, use Calendar class to write your method to find a number of months and year between given dates, second, use Joda-time library and the third option is to upgrade to Java 8. Read more �

Why Timestamp cannot be used in place of Date in Java?

One of the tricky question from Java Interview is, "Can we pass a Timestamp instance to a method expecting java.util.Date? " , it's a tricky question because the answer is both Yes and No . You can, of course, pass a Timestamp object to a method with the argument as Date because first, Timestamp is a subclass of java.util.Date and second it contains both date and time values which are missing in either java.sql.Date and java.sql.Time. So there is more reason to pass a Timestamp value to Date but you should not be doing that. Why? because Timestamp is not exactly Date. It's a composite type of java.util.Date and an additional nanosecond value which is fitted there to confirm database DATETIME data type, which supports nanosecond precision. If you look at the implementation of java.sql.Timestamp class, you will find that the long value supplied by Date is stored separately then this nanosecond value. Read more �

How to add, subtract days, months, years, hours from Date and Time in Java

Adding days, hours, month or years to dates is a common task in Java. java.util.Calendar can be used to perform Date and Time arithmetic in Java. Calendar class not only provides date manipulation but it also support time manipulation i.e. you can add, subtract hours, minutes and seconds from current time. Calendar class automatically handles date transition or month transition for example if you ask date after 30 days it will return you date based on whether current month is 30 or 31 days long. Same is true in case of adding and subtracting years, Calendar takes care whether current or following year is a leap year or not. For example 2012 is a leap year and it has February with 29 days, if you ask Calendar day before 365 it will return 24th July (assuming current date 23 rd July) which shows it take care of leap year. By the way there are couple of more date and time related articles e.g. How to find current date and time in Java and How to convert Date to String in Java . If you

How to convert milliseconds to Date in Java - tutorial example

Do you want to convert milliseconds to Date in Java ? Actually java.util.Date is internally specified in milliseconds from epoch. So any date is number of millisecond passed since January 1, 1970, 00:00:00 GMT and Date provides constructor which can be used to create Date from milliseconds . Knowing the fact that Date is internally maintained in milliseconds allows you to store date in form of millisecond in Server or in your Class because that can be effectively expressed with a long value . In fact many experienced Java programmer store Date as long value while writing Immutable class which requires Date, one of the reason for that is Date being mutable and long value of Date can be very handy. By the ways this is next in Date related article, we have already discussed How to convert String to Date and How to get current month, year and day of week from Date in Java . If you haven�t read them already, you may find them useful. In this Java tutorial we will see example of converti

How to get current date, month, year and day of week in Java program

Here is quick Java tip to get current date, month, year and day of week from Java program. Java provides a rich Date and Time API though having thread-safety issue but its rich in function and if used locally can give you all the date and time information which you need for your enterprise Java application. In last Java tutorial on Date and Time API we have seen how to get current date and time from different timezone using DateFormat and SimpleDateFormat classes and in this post we will get some more details like current month, year, day of week etc by using java.util.Calendar class. Just keep in mind that Calendar instance should not be shared between multiple threads. Calendar class in Java provides different fields to get different information e.g. Calendar.DATE gives you current date while Calendar.MONTH gives you current month based on what type of Calendar you are using, which depends upon locale. Read more �