Showing posts with label SimpleDateFormat. Show all posts
Showing posts with label SimpleDateFormat. Show all posts

Monday, September 7, 2026

How to Get the Day of the Week from a Date in Java

There are many ways to find the day name from a given date. But here I have used simple one.Really its so easy but sometimes we hang on it .We never found the solutions.Today its happening with me, because I always prefer  less search on Google.

Below the code for finding the day name from an input date.import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DayFinder {
    public static void main(String[] args) {
        String inputDate = "01/08/2012";
        SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");

        try {
            Date dt1 = format1.parse(inputDate);
            DateFormat format2 = new SimpleDateFormat("EEEE");
            String finalDay = format2.format(dt1);
            System.out.println("My Day is: " + finalDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}


Output:- Wednesday

Explanation :- 

First I prepare a date by using SimpleDateFormat . Then Prepare a DateFormat by using that before SimpleDateFormat(format1).

EEEE is the date Format Suffix for Day.More
 

Modern Java Solution (java.time)

In modern Java, this is simpler, safer, and does not need Date or SimpleDateFormat:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DayFinderModern {
    public static void main(String[] args) {
        String inputDate = "01/08/2012";
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date = LocalDate.parse(inputDate, formatter);

        // Get day of the week directly
        System.out.println("My Day is: " + date.getDayOfWeek()); 
        // Or format it as full text ("Wednesday"):
        // date.format(DateTimeFormatter.ofPattern("EEEE"))
    }

 

Friday, November 11, 2016

How to Convert a String Date to Java Calendar

How to convert String date to Java Calendar.


This can be done by some other way also, but  I have done in the below format. Hope this is will help you.

 

Example :- 
Input String Date - "20160829T23:59:59Z"

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

public class StringDateToCalendar {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
            String string = "20160829T23:59:59Z";
            String pattern = "yyyyMMdd'T'HH:mm:ss'Z'";
            Date date = new SimpleDateFormat(pattern).parse(string);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            System.out.println("Calendar is :: "+ calendar.getTime());
    }

}
Output :-
Calendar is :: Mon Aug 29 23:59:59 AEST 2016

How to Convert a String Date to Java Date

This can be done by some other way also, but  I have done in the below format. Hope this is will help you.

Example :- 

Input String Date - "20160829T23:59:59Z"


import java.text.SimpleDateFormat;
import java.util.Date;

public class StringDateToCalender {
    public static void main(String[] args) throws Exception {
        String string = "20160829T23:59:59Z";
        String pattern = "yyyyMMdd'T'HH:mm:ss'Z'";
        Date date = new SimpleDateFormat(pattern).parse(string);
        System.out.println(date);
    }
}

Output :-
Converted Date is :: Mon Aug 29 23:59:59 AEST 2016