Showing posts with label java.util.Date. Show all posts
Showing posts with label java.util.Date. Show all posts

How to convert DateTime to different Timezone's

Convert the given date, time to a different timezone along with 12-hour clock format. Lets see simple java example to convert the given string to Date and with local/Default(IST) time to 2 different timezone along with 12-hour clock format (AM/PM).

Convert Date/Time for given Timezone - java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTime {

 public static void main(String[] args) {
  String sDate1="25-03-2018 13:34:56";  
  SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  // Converting string to Date() with specified date format.
  Date date = null;
  try {
   date = sdf.parse(sDate1);
  } catch (ParseException e) { 
   // If any format error pick current datetime
   date = new Date();
  sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
  Calendar calendar = Calendar.getInstance();
  System.out.println("Default IST Time      : "+sdf.format(calendar.getTime()));    
  // Changing TimeZone to America/New_York
  System.out.println("America/New_York Time : "+sdf.format(calendar.getTime()));    

  // Changing TimeZone to Australia/Sydney
  System.out.println("Australia/Sydney Time : "+sdf.format(calendar.getTime()));


Default IST Time      : 25-Mar-2018 01:34:56 PM
America/New_York Time : 25-Mar-2018 04:04:56 AM
Australia/Sydney Time : 25-Mar-2018 07:04:56 PM

java.util.Date to java.sql.Date with timestamp

java.sql.Date() class mainly used in JDBC API. If we need to set date value or need to get date from JDBC result-set then we need to go for java.sql.Date(). Actually java.sql.Date() class extends java.util.Date() class and we can do all operations which we used with java.util.Date() class. Only one main difference between these classes are java.sql.Date() class hold only date and timestamp. Even if try to call methods like getHours(), getMinutes() and getSeconds() which will throw exception like

 at java.sql.Date.getHours(......)

In that case if we need date along with timestamp to deal with JDBC API we can use java.sql.timestamp() class. A thin wrapper around java.util.Date() that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds. A Timestamp also provides formatting and parsing operations to support the JDBC escape syntax for timestamp values.

Now lets see simple example to get current date and time in java.sql.Date() and convert to java.sql.Date() and java.sql.Timestamp()

public class DateTimeTesting {

 public static void main(String[] args) {
  java.util.Date date = new java.util.Date();
  System.out.println("Util DATE       : "+date);
  java.sql.Date sqlDate = new java.sql.Date(date.getTime());
  System.out.println("SQL DATE        : "+sqlDate);
  java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(date.getTime());
  System.out.println("SQL DATE & TIME : "+sqlDateTime);


java.util.Date to java.sql.Date with timestamp