Showing posts with label Date and Time. Show all posts
Showing posts with label Date and Time. 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) { 
   e.printStackTrace();
   // If any format error pick current datetime
   date = new Date();
  } 
  sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);
  
  System.out.println("Default IST Time      : "+sdf.format(calendar.getTime()));    
  
  
  // Changing TimeZone to America/New_York
  sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
  System.out.println("America/New_York Time : "+sdf.format(calendar.getTime()));    

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


OUTPUT:


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 Date and Time interview questions




In this tutorial we will see about using Java Date and Time-stamp interview questions as,
1. Getting current local Date and Time
2. Converting current local Date and Time to GMT Date and Time
3. Convert String to Date and Time
4. Convert Date and Time to String
5. Adding N days with current Date and Time
6. Subtracting N days with current Date and Time
7. Change Date and Time format
8. Calculate difference between 2 Date
9. Compare 2 Dates

Lets start with simple examples for all above question on Java Date and Time. In all examples we will be using
SimpleDateFormat()
parse()
format()



1. Getting current local Date and Time

public class MyDateTimeTest {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("Current Date & Time : "+date);
    }
}

OUTPUT:

Current Date & Time : Sun Mar 17 01:17:30 IST 2013



2. Converting current local Date and Time to GMT Date and Time

public class MyDateTimeTest {
    public static void main(String[] args) {
        Date date = new Date();
        DateFormat gmtFor = new SimpleDateFormat();
        TimeZone gmtTime = TimeZone.getTimeZone("GMT");
          
        gmtFor.setTimeZone(gmtTime);
        System.out.println("Current Date & Time : "+date.toString());
        System.out.println("GMT Time & Date     : " + gmtFor.format(date));
    }
}

OUTPUT:


Current Date & Time : Sun Mar 17 01:29:09 IST 2013
GMT Date & Time     : 3/16/13 7:59 PM



3. Convert String to Date and Time

public class MyDateTimeTest {
    public static void main(String[] args) {
        String myDateTime = "2013-03-17 12:45:56";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date convertedDateTime = df.parse(myDateTime);
            System.out.println("DATE : "+df.format(convertedDateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

OUTPUT:
DATE : 2013-03-17 12:45:56




4. Convert Date and Time to String

public class MyDateTimeTest {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date convertedDateTime = new Date();
        String dateInString = df.format(convertedDateTime);
        System.out.println("DATE : "+dateInString);
    }
}

OUTPUT:

DATE : 2013-03-17 02:03:09



5. Adding N days with current Date and Time 

public class MyDateTimeTest {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDateTime = new Date();
        Date newDate = null;
        try {
            newDate = df.parse(df.format(currentDateTime));
            newDate = addNDays(newDate, 10); // Adding 10 days with current Date
            System.out.println("CURRENT DATE              : "+df.format(currentDateTime));
            System.out.println("DATE AFTER ADDING 10 DAYS : "+df.format(newDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    public static Date addNDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); 
        return cal.getTime();
    }
}

OUTPUT:

CURRENT DATE              : 2013-03-17 02:17:24
DATE AFTER ADDING 10 DAYS : 2013-03-27 02:17:24



6. Subtracting N days with current Date and Time 

public class MyDateTimeTest {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDateTime = new Date();
        Date newDate = null;
        try {
            newDate = df.parse(df.format(currentDateTime));
            newDate = addNDays(newDate, 10); // Subtracting 10 days with current Date
            System.out.println("CURRENT DATE                   : "+df.format(currentDateTime));
            System.out.println("DATE AFTER SUBTRACTING 10 DAYS : "+df.format(newDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    public static Date addNDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days*-1); 
        return cal.getTime();
    }
}

OUTPUT:

CURRENT DATE                   : 2013-03-17 02:20:12
DATE AFTER SUBTRACTING 10 DAYS : 2013-03-07 02:20:12



7. Change Date and Time format 

public class MyDateTimeTest {
    public static void main(String[] args) {
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df2 = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss aa");
    
        Date currentDateTime = new Date();
        System.out.println("CURRENT DATE      : "+df1.format(currentDateTime));
        System.out.println("NEW FORMATED DATE : "+df2.format(currentDateTime));        
    }
}

OUTPUT:

CURRENT DATE      : 2013-03-17 02:25:59
NEW FORMATED DATE : 17/Mar/2013 02:25:59 AM



8. Calculate difference between 2 Date 

public class MyDateTimeTest {
    public static void main(String[] args) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String otherDate = "2013-03-15 02:38:45";
        
        Date date1 = new Date();
        Date date2 = df.parse(otherDate);
        
        System.out.println("DATE-1 : "+df.format(date1));
        System.out.println("DATE-2 : "+df.format(date2));    
        long diff = date1.getTime() - date2.getTime();
        
        System.out.println("DATE DIFF : "+(diff/ (1000 * 60 * 60)) +" hours");
    }
}

OUTPUT:

DATE-1 : 2013-03-17 02:38:54
DATE-2 : 2013-03-15 02:38:45
DATE DIFF : 48 hours



9. Compare 2 Dates

public class MyDateTimeTest {
    public static void main(String[] args) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String otherDate = "2013-03-15 02:38:45";
        
        Date date1 = new Date();
        Date date2 = df.parse(otherDate);
        
        System.out.println("DATE-1 : "+df.format(date1));
        System.out.println("DATE-2 : "+df.format(date2));    
        
        if(date1.compareTo(date2) > 0){
                System.out.println("Date-1 is after Date-2");
            }else if(date1.compareTo(date2) > 0){
                System.out.println("Date-1 is before Date-2");
            }else if(date1.compareTo(date2) == 0){
                System.out.println("Date-1 is equal to Date-2");
            }
        }



OUTPUT:
DATE-1 : 2013-03-17 02:42:37
DATE-2 : 2013-03-15 02:38:45
Date-1 is after Date-2