Postingan

Menampilkan postingan dengan label date and time tutorial

6 Date and Calendar Details Every Java Programmer Should Know

Gambar
Hello guys, to appreciate the brilliance of the new Date and Time API introduced in Java 8, you must remember nasty problems you have faced. Another reason for this article is that even though Java is ready with the new, shiny, and carefully designed Date and time API, your project is probably not. Most of the prominent organizations that use Java at large scales, like Investment banks, Insurance companies, major E-commerce giants, are quite reluctant to upgrade. There are many firms, both big and small that are still running on Java 5 and expect to migrate to Java 6 in a couple of years. If you are working as a professional Java developer, you are expected to maintain and support those projects running in an older version of Java with the old date and calendar API, and having these details under your belt will only going to help you write better code in Java. On a different note, I have found that Java developers know very little about Date and Calendar classes compared to other impor...

How to Convert Date to String in Java with Example

Gambar
Some times we need to convert java.util.Date to string in java may for displaying purpose I need it that while working on displaying then I thought about this article to list down various way of converting this in various ways after some reading I found that SimpleDateFormat makes this quite easy. To get the feel of Date API in java and to familiarize ourselves with these classes we will see different examples of converting date to String in our application. Both DateFormat and SimpleDateFormat class belongs to java.text package and they are very powerful and you can use them for conversion. it also good for parsing a string into a date and can be used to show in various locales also. Steps for converting Date to String in Java It's very easy with the use of SimpleDateFormat, here are the exact steps: 1) The first step is to create a date format using SimpleDateFormat class 2) Call format() method of SimpleDateFormat bypassing Date object this will return String representation of ...

How to convert String to Date in Java - SimpleDateFormat Example

Gambar
SimpleDateFormat in Java can be used to convert String to Date in Java. java.text.SimpleDateFormat is an implementation of DateFormat which defines a date pattern and can convert a particular String which follows that pattern into Date in Java.This is the second part of the article on java.util.Date and String in Java. In the first part, we have seen How to convert Date to String in Java. SimpleDateFormat accepts a String in any date format e.g. yyyyMMdd is a date pattern and  20110924 is a String in that format. Now you want to create a java.util.Date object from this String. In this Java tutorial, we will see list steps to convert String to Date in Java and then we will see different examples of SimpleDateFormat with different date patterns e.g. ddMMyy or dd-MM-yyyy. Though converting String to Date is quite easy using SimpleDateFormat, but you need to remember that SimpleDateFormat is not thread-safe, which means you can not share the same instance of SimpleDateFormat between mu...

How to convert java.util.Date to java.sql.Date - JDBC Example

Gambar
You often need to convert java.util.Date to java.sql.Date if you are storing dates in database e.g. SQL SERVER or MySQL. Since JDBC has their own data types for date and time e.g. java.sql.Date , java.sql.Time and j ava.sql.TimeStamp to match with database date, time and date-time types, you cannot pass a java.util.Date directly. All methods which are suppose to store dates e.g. setDate(paramName, paramValue) expects java.sql.Date, so it becomes essential to know how to convert java.util.Date to java.sql.Date in JDBC . You would be surprised to know that java.sql.Date is a subclass of java.util.Date and all it does is suppress or remove time-related fields from java.util.Date. It is also a good example of a class which violates Liskov substitution principle, because even though java.sql.Date extends java.util.Date, you cannot pass around it to the method which expect java.util.Date because all time-related methods e.g. getHour(), getMinute() and getSeconds() method throws java.util...