Date manipulation in Java

How to determine the day of the week by passing a specific date?

1). Using calendar Class

Calendar c = Calendar.getInstance(); c.setTime(yourDate); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

2). Using SimpleDateFormat

String input_date="14/07/2020"; SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy"); Date dt1=format1.parse(input_date); DateFormat format2=new SimpleDateFormat("EEEE"); String finalDay=format2.format(dt1);

3). Using DateTimeFormatter

int dayOfWeek = localDate.getDayOfWeek(); // Follows ISO 8601 standard, where Monday = 1, Sunday = 7. Locale locale = Locale.US; // Locale specifies the human language to use in determining day-of-week name (Tuesday in English versus Mardi in French). DateTimeFormatter formatterOutput = DateTimeFormat.forPattern( "E" ).withLocale( locale ); String output = formatterOutput.print( localDate ); // 'E' is code for abbreviation of day-of-week name. See Joda-Time doc. String outputQuébécois = formatterOutput.withLocale( Locale.CANADA_FRENCH ).print( localDate ); System.out.println( "input: " + input ); System.out.println( "localDate: " + localDate ); // Defaults to ISO 8601 formatted strings. System.out.println( "dayOfWeek: " + dayOfWee

Get year, month and day from a given Date using Using Java 7

1). Using java util package and classes like Date and Calendar we can get a year, month, and day from a given date.

We create a date and Calendar Instance here

Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date);

Get year in integer

We need to pass Calendar.YEAR to the calender.get Method.

int year = calendar.get(Calendar.YEAR);

Get month in integer

We need to pass Calendar.MONTH to calender.get Method.

int month = calendar.get(Calendar.MONTH);

Get day in integer

We need to pass Calendar.DAY_OF_MONTH to the calender.get Method.

int day = calendar.get(Calendar.DAY_OF_MONTH);

package com.javacodestuffs.core.datetime; import java.util.Calendar; import java.util.Date; import java.time.LocalDate; import java.text.SimpleDateFormat; public class LocalDateExample1 { public static void main(String[] args) { Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.println("year : " + year); System.out.println("month : " + month); System.out.println("day : " + day); } } output: year : 2020 month : 6 day : 14

Using Java 8 to get Date details

Using Java 8 makes our life easier while manipulating DateTime.

The java.time package contains a number of classes that are used for manipulation of Date.

Java LocalDate class is an immutable class that represents the Date with a default format of yyyy-MM-dd. It inherits Object class and implements the ChronoLocalDate interface.

LocalDate date = LocalDate.now();

1). Get Year

localDate.getYear();

2). Get Month

localDate.getMonthValue();

3). Get Day

localDate.getDayOfMonth();

Here is the complete program

package com.javacodestuffs.core.datetime; import java.util.*; import java.time.LocalDate; import java.text.SimpleDateFormat; public class LocalDateExample2 { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); int year = localDate.getYear(); int month = localDate.getMonthValue(); int day = localDate.getDayOfMonth(); System.out.println("year: " + year); System.out.println("month: " + month); System.out.println("day: " + day); } } output: year: 2020 month: 7 day: 14

Get DayOfWeek using the switch

package com.javacodestuffs.core.datetime; import java.util.*; public class DayOfWeek { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DAY_OF_WEEK); System.out.print("Today is : "); switch (day) { case 1: System.out.print("Sunday"); break; case 2: System.out.print("Monday"); break; case 3: System.out.print("Tuesday"); break; case 4: System.out.print("Wednesday"); break; case 5: System.out.print("Thursday"); break; case 6: System.out.print("Friday"); break; case 7: System.out.print("Saturday"); } } } output: Today is : Tuesday

Java SimpleDateFormat to get date in ("yyyy-MM-dd'T'HH:mm:ss'Z'") format

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT"));//UTC String currentDate1 = sdf22.format(date1);

package com.javacodestuffs.core.datetime; import java.util.* ; import java.time.LocalDate; import java.text.SimpleDateFormat; public class DateInUTCFomat { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //UTC LocalDate date = LocalDate.now(); System.out.println("The Date in UTC format is : "+sdf.format(new Date())); } } output: The Date in UTC format is : 2020-07-14T19:08:27Z

Post/Questions related to Date manipulation in Java

How to parse String to LocalDateTime in Java 8?
How to calculate the difference between two dates in Java?
25 Java Date, Time, and Calendar based Questions from Interviews
How to change the date format of String in Java 8?
How to compare two Dates in Java 8?
How to convert java.util.Date to java.time.LocalDateTime in Java 8?
How do you get the day of the week from a date in Java?
How to convert old Date to new LocalDate in Java 8?
15 Examples to format and parse Date in Java 8?
How to convert String to LocalDateTime in Java 8?
How to convert Timestamp to Date in Java?
15 Examples to learn new Date and Time API in Java 8
How to get date before 6 months in java?
How to get day month and year from date in java?
How to get month from date in java?
How to get previous month date from current date in java?
How to get first day of month in java?
How to get last day of month in java?
How to get first Sunday of month?
How to get first Sunday of Year?
How to get first Sunday of Last Year?
How to get month name from date in java?
How to get number of days in a month in java?
How to get number of weeks in a month using java?

In this article, we have seen the   Date manipulation in Java with examples. All source code in the article can be found in the GitHub repository.