wellnwill Marathahalli Bangalore

Java date


Date class is a predefined class which is available inside java.util package. SimpleDateFormat class is used to convert String to Date in Java by using parse() of this class.

import java.util.*;  
import java.text.SimpleDateFormat;  
public class StringToDate 
{  
 public static void main(String[] args)throws Exception 
 {  
    Scanner scn=new Scanner(System.in);
    System.out.println("enter date:in dd-MM-yyyy format");
    String s=scn.next();  
   //creating SimpleDateFormat class object with some format
    SimpleDateFormat sdt=new SimpleDateFormat("dd-MM-yyyy");
    Date dt=sdt.parse(s);  //parsing "dd-MM-yyyy" to date
    System.out.println("The input date is +"+"\t"+ dt);  
 }  
}

Before java 8, we were using Date and Calendar classes which are not thread safe means we cannot share same instance with multiple threads.

To avoid concurrency problems, we have to write some additional code. The new Date API is immutable and thread safe.

It was difficult to handle time zone concept in old Date API. We can use Local and Zoned Date/Time API to handle time zone easily through new Date API.

import java.util.Date;
class Test
{
public static void main(String args[])
 {
  Date d1=new Date();
  System.out.println(d1.getDate()+"\t"+d1.getMonth()+"\t"+d1.getYear());
  System.out.println(d1.getDate()+"\t"+(d1.getMonth()+1)+"\t"+(d1.getYear()+1900));
  System.out.println(d1.getHours()+"\t"+d1.getMinutes()+"\t"+d1.getSeconds());
 }
}

LocalDate, LocalTime, LocalDateTime

These classes are present inside java.time package and used to get date/time from the system clock.

import java.time.*;
class Test{
public static void main(String args[]) {
  LocalDate todayDate = LocalDate.now(); 
  System.out.println("Today's local date: " + todayDate);
   
  int yy = todayDate.getYear(); 
  int mm = todayDate.getMonthValue();
  int dd = todayDate.getDayOfMonth();
  System.out.println("year:"+yy+" "+"month:"+mm+" "+"date:"+dd);

  LocalTime todayTime = LocalTime.now();
  System.out.println("Today's local time: " + todayTime);
  LocalDateTime date_time=LocalDateTime.now();
  System.out.println("Today's local date_time: " + date_time);
  }
}
import java.time.*;
class Test
{
public static void main(String args[])
 {
  LocalDate t1 = LocalDate.of(2012,9,27);
  LocalDate t2 = LocalDate.of(2012,9,28);
  LocalDate t3 = LocalDate.of(2012,9,27);
  System.out.println(t1.isAfter(t2));
  System.out.println(t1.isBefore(t2));
  System.out.println(t1.equals(t3));
  }
}

We have several date time format even we can create our own date time format by using static method ofPattern() of DateTimeFormatter class.


import java.text.*;
import java.time.*;
import java.util.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

class Test
{
public static void main(String args[])
 {
   Scanner scn=new Scanner(System.in);
   System.out.println("enter date:in dd-MM-yyyy format");
   String sdate=scn.next();  

 try
  { 
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MM-yyyy"); 
    LocalDate d1= LocalDate.parse(sdate, fmt); 
    System.out.println("date in Date format:"+d1);
   } 
 catch (DateTimeParseException e) 
 { 
   System.out.printf("invalid format:"+sdate); 
   e.printStackTrace(); 
 }
}
}


Date to String Conversion


import java.time.*;
import java.util.*;
import java.time.format.DateTimeFormatter;
class Test
{
public static void main(String args[])
 {  
  LocalDateTime date_time  = LocalDateTime.now();
 
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM dd yyyy"); 
    String sdate= date_time.format(fmt); 
    System.out.println("date in String format:"+sdate);
 }
}

Footer with Map

Top