Date Class :
·
The Date class is a predefined class in Java.
·
It can be used to get the current system date
and time.
·
To use the Date class, import the java.util
package.
package DateBasics;
import java.util.Date;
public class DateBasics1 {
public
static void main(String[] args) {
//Create
obj for Date class
Date
d = new Date();
//
import java.util pkg
System.out.println("Date
=" + d);
//
Date =Thu May 02 08:29:52 IST 2024
// get
date
int
dd = d.getDate();
System.out.println("dd=
"+ dd);// dd= 2
//
get month
int
mm = d.getMonth();
System.out.println("mm="+
mm);// mm=4 actual month-1 May = 5 th month = 5-1 =4
//
display actualMonth
mm
= mm +1;
// 4+1
// 5
System.out.println("actualMonth="+mm);//
actualMonth=5
//
get year
int
yy = d.getYear();
// 124
// 2024
// - 124
///-----------------------------
// 1900
System.out.println("yy="+
yy);// expecting -2024 //actual = yy=124 **Care
//
actual year =
int
actualYear = yy + 1900;
// 124 + 1900
// 2024
// actualYear=
2024
System.out.println("actualYear
="+actualYear);
//
1900
// 2024
// 124
// 1900
//
// date =Date =Fri May 19 08:51:24 IST 2023
//HW
get hours
//
HW get minutes
//HW
get seconds
}
}
o/p:
Date =Thu May 02 08:37:12 IST
2024
dd= 2
mm=4
actualMonth=5
yy=124
actualYear =2024
SimpleDateFomat Basics:
·
SimpleDateFormat is a predefined class in Java.
·
It can be used to display given date in diff
formats
//
date =Fri Jan 13 09:39:15 IST 2023
// dd-MM/yyyy
, MM--dd-yyyy, yyyy-MM-dd
//
MM - Months
·
It can be used to format and parse dates into
various formats.
package DateBasics;
import
java.text.SimpleDateFormat;
import java.util.Date;
public class
SimpleDateFromatBasics1 {
public
static void main(String[] args) {
//
display date in format - dd-MM-yyyy
// create
obj for SimpleDateFormat class and pass diff formats
SimpleDateFormat
sdf = new SimpleDateFormat("dd-MM-yyyy");
//
import java.text.SimpleDateFormat;
//create
obj for Date class
Date
d = new Date();
//
import java.util.Date;
System.out.println("d="+
d);// d=Thu May 02
08:43:16 IST 2024
//format
date and dispay
// format()
can be used to convert Date class obj to String value
String
ddMMyyyy = sdf.format(d);
// // 02-05-2024
//// dd-MM-yyyy
System.out.println("ddMMyyyy="+
ddMMyyyy);
// ddMMyyyy=
02-05-2024
// disply date in MM--dd-yyyy,
05-02-2024
//
format and display
sdf
= new SimpleDateFormat("MM-dd-yyyy");
// d=Fri May 19 09:09:27 IST 2023
// format()
can be used to convert Date class obj to String value
String
MMddyyy = sdf.format(d);
// 05-02-2024
System.out.println("MMddyyy="+MMddyyy);//
05-02-2024
//
HW display date in yyyy-MM-dd
//
HW display date in dd/MM/yyyy
// HW
display date in dd:MM:yyyy
//
display date in mm-dd-yyyy -//
mm - minutes MM- Month
// 08:48:50
hh:mm:ss MM-Month
sdf = new
SimpleDateFormat("mm-dd-yyyy");
String mmddyyyy = sdf.format(d);
System.out.println("mmddyyyy ="+mmddyyyy);
// mmddyyyy = 50-02-2024
//
50 respresents minutes
//HW
display date in format -MMM-dd-yy Jan, Feb
//
HW Display get hour, min Seconds
// hh:mm:s
}
}
Parse():
//Parse
() can be used to convert String date to
Date class obj
Converts
a date string (e.g., "13-01-2023") into a Date object based on the specified format (e.g., dd-MM-yyyy).
package DateBasics;
import java.text.ParseException;
import
java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFromatBasics2
{
public
static void main(String[] args) throws ParseException {
//
create Date class obj
Date
d = new Date();
//
import java.util.Date;
System.out.println("date="
+ d);// Thu May 02 08:53:24 IST 2024
//
create obj for SimpleDateFormat -class and pass MMM-dd-yy
SimpleDateFormat
sdf = new SimpleDateFormat("MMM-dd-yy");
String MMddyyyy= sdf.format(d);
System.out.println("MMddyyyy
="+MMddyyyy);
// May-02-24 2024
//Parse
() : can be used to convert String
date to Date class obj
String
sdate= "17/01/2023";
// String sdate=
"01/17/2023"; // Dont pass the data in this format
//
throws ParseException
// Date d2 = sdf.parse(sdate);
// Date
d2 =sdf.parse(sdate); //Exception
in thread "main" java.text.ParseException: Unparseable date:
"17/01/2023"
// System.out.println("d2
="+d2);// d2 =
// if u pass any invalid data (String ) in
parse, it throws ParseException: Unparseable date:
Date
d3 = sdf.parse(MMddyyyy) ;// May-02-24
// Thu May 02 00:00:00 IST 2024
//
System.out.println("d3="+
d3);//d3=Thu May 02 00:00:00 IST 2024
}
}
Calendar:
·
Calendar is a predefined class in Java.
·
It is a part of the java.util package.
·
Used to perform operations related to dates and
times, including manipulating and retrieving calendar fields.
package DateBasics;
import java.util.Calendar;
import java.util.Date;
public class CalendarBasics {
public
static void main(String[] args) {
//
Calendar: predefined class in java
// Calendar cal = new Calendar();
// Cannot instantiate the type Calendar
Calendar
cal = Calendar.getInstance();
System.out.println("Cal
="+cal);
//java.util.GregorianCalendar[time=1714621029594,areFieldsSet=true,areAllFieldsSet=true,
//lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,
//useDaylight=false,transitions=7,lastRule=null],
//firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,
//YEAR=2024,MONTH=4,WEEK_OF_YEAR=18,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=123,
//DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=9,HOUR_OF_DAY=9
//,MINUTE=7,SECOND=9,MILLISECOND=594,ZONE_OFFSET=19800000,DST_OFFSET=0]
//
get data and time - getTime()
Date
d = cal.getTime();
System.out.println("d="+
d); //d=Thu May 02 09:09:11 IST 2024
//
// add 2 days to current system data
cal.add(Calendar.DAY_OF_YEAR,
2);
// cal.add(Calendar.DAY_OF_MONTH,
2);
d
= cal.getTime();
System.out.println("d="
+ d);// d=Sat May 04 09:13:34 IST 2024
//
add 2 months to current sys stem date
cal.add(Calendar.MONTH,
2);
d
= cal.getTime();
System.out.println("d="+d);
//
d1=d=d=Tue Jul 02 09:17:49 IST 2024
// HW add
3 years to current system date
cal.add(Calendar.YEAR,
3);
d
= cal.getTime();
System.out.println("d="+d);//
d=Fri Jul 02 09:19:01 IST 2027
//
substract 1 day from current sysstem
date
cal.add(Calendar.DAY_OF_MONTH,
-1);
d
= cal.getTime();
System.out.println("d="+d);
//
d=
//
HW substact 2 month
//
HW Substract 3 years
}
}
o/p:
Cal
=java.util.GregorianCalendar[time=1714622143096,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2024,MONTH=4,WEEK_OF_YEAR=18,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=123,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=25,SECOND=43,MILLISECOND=96,ZONE_OFFSET=19800000,DST_OFFSET=0]
d=Thu May 02 09:25:43 IST 2024
d=Sat May 04 09:25:43 IST 2024
d=Thu Jul 04 09:25:43 IST 2024
d=Sun Jul 04 09:25:43 IST 2027
d=Sat Jul 03 09:25:43 IST 2027
No comments:
Post a Comment