Modify Date and Time from GregorianCalendar in Java

karthikeya Boyini
Updated on 27-Jun-2020 09:33:19

833 Views

For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Firstly, let us display the current date and time.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime());Now, modify the date. Here we are adding two days to the month using the add() method.cal.add((GregorianCalendar.MONTH), 2);Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();       System.out.println("Current date: " + cal.getTime());       cal.add((GregorianCalendar.MONTH), 2);       System.out.println("Modified date: " + cal.getTime());    } }OutputCurrent date: Mon Nov 19 17:52:55 UTC 2018 Modified date: Sat Jan ... Read More

Display Previous Year from GregorianCalendar in Java

Samual Sam
Updated on 27-Jun-2020 09:32:26

170 Views

For GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create an object.GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();Now, use the following field and add() method with a negative one (-1) to display the previous year.cal.add((GregorianCalendar.YEAR), -1)Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();       System.out.println("Current date: " + cal.getTime());       // previous year       cal.add((GregorianCalendar.YEAR), -1);       System.out.println("Modified date: " + cal.getTime());    } }OutputCurrent date: Mon Nov 19 18:05:49 UTC 2018 Modified date: Sun Nov 19 18:05:49 UTC 2017

Parse Time Using Custom Format in Java

Samual Sam
Updated on 27-Jun-2020 09:30:10

224 Views

To get the time format, use the DateFormat class and create a new object.DateFormat dateFormatter = new SimpleDateFormat("hh.mm.ss a");Now, parse the time.dateFormatter.parse("12.55.20 PM");Example Live Demoimport java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Main {    public static void main(String[] argv) throws Exception {       DateFormat dateFormatter = new SimpleDateFormat("hh.mm.ss a");       System.out.println("Parse Time...");       Date dt = (Date) dateFormatter.parse("12.55.20 PM");       System.out.println(dt);    } }OutputParse Time... Thu Jan 01 12:55:20 UTC 1970

Parse String Date Value Input in Java

karthikeya Boyini
Updated on 27-Jun-2020 09:29:14

232 Views

UseSimpleDateFormat('dd-MMM-yy') for string date.Format dateFormatter = new SimpleDateFormat("dd-MMM-yy");For above class, do not forget to import the following package, else an error would be visible.import java.text.SimpleDateFormat;Now, parse the date.Date dt = (Date) dateFormatter.parseObject("20-Nov-18");Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Main {    public static void main(String[] argv) throws Exception {       Format dateFormatter = new SimpleDateFormat("dd-MMM-yy");       // parse       Date dt = (Date) dateFormatter.parseObject("20-Nov-18");       System.out.println("Date = "+dt);    } }OutputDate = Tue Nov 20 00:00:00 UTC 2018

What are Sitemaps and How to Create Them

Prasanna Kotamraju
Updated on 27-Jun-2020 09:27:28

339 Views

A sitemap is the map of a website showing the location of pages. These sitemaps are formatted for Search Engine Optimization and to streamline the user experience.There are different types of sitemaps. They can be Standard sitemaps, Content-based such as videos, images etc., How to Create a SitemapThere are a lot of plugins available that automatically create a sitemap. People who use WordPress can create a sitemap with a few simple clicks. For simple websites, these plugins work fine.There are very good options for SEO for WordPress by Yoast, which has a built-in sitemap generator, or Google XML Sitemaps Generator ... Read More

Microprocessor Basics

Chandu yadav
Updated on 27-Jun-2020 09:24:10

4K+ Views

The processor on a single chip is called a Microprocessor which can process micro-instructions. Instructions in the form of 0s and 1s are called micro-instructions. The microprocessor is the CPU part of a microcomputer, and it is also available as a single integrated circuit. Thus as main components, the microprocessor will have the Control Unit (CU) and the Arithmetic Logic Unit (ALU) of a microcomputer. An example is Intel 8085 microprocessor. In addition to the microprocessor features, a microcomputer will have the following additional features −ROM / PROM / EPROM / EEPROM for storing programRAM for storing data, intermediate results, ... Read More

Gregorian Calendar in Java

Samual Sam
Updated on 27-Jun-2020 09:13:00

1K+ Views

GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, which corresponds by default to the Gregorian date when the Gregorian calendar was instituted.The java.util.GregorianCalendar class in Java is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.Import the following package to work with GregorianCalendar class.import java.util.GregorianCalendar;The following are the constructors.Sr.No.Constructor & Description1GregorianCalendar() This constructs a default GregorianCalendar using the current time in the default time zone with the default locale.2GregorianCalendar(int year, int month, int dayOfMonth) This constructs a GregorianCalendar with the given ... Read More

Specify Timezone Explicitly for Gregorian Calendar in Java

karthikeya Boyini
Updated on 27-Jun-2020 09:08:31

225 Views

To specify the TimeZone explicitly, use the getTimeZone() method of the TimeZone class. For Locale and TimeZone, we have imported the following packages.import java.util.Locale; import java.util.TimeZoneLet us specify for timezone America/New_York.GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"), Locale.US);The following is an example.Example Live Demoimport java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; public class Demo {    public static void main(String[] a) {       GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"), Locale.US);       System.out.println(cal);    } }Outputjava.util.GregorianCalendar[time=1542643323673, areFieldsSet=true, areAllFieldsSet=true, lenient=true, zone=sun.util.calendar.ZoneInfo[id="America/New_York", offset=-18000000, dstSavings=3600000, useDaylight=true, transitions=235, lastRule=java.util.SimpleTimeZone[id=America/New_York, offset=-18000000, dstSavings=3600000, useDaylight=true, startYear=0, startMode=3, startMonth=2, startDay=8, startDayOfWeek=1, startRead More

Set Date and Time with Gregorian Calendar in Java

Samual Sam
Updated on 27-Jun-2020 09:07:16

2K+ Views

To work with the GregorianCalendar class, import the following package.import java.util.GregorianCalendar;Create a Date object.Date d = new Date();Now, create an object and set the time using the setTime() method.GregorianCalendar cal = new GregorianCalendar(); cal.setTime(d);The following is an example.Example Live Demoimport java.util.Date; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] a) {       Date d = new Date();       GregorianCalendar cal = new GregorianCalendar();       cal.setTime(d);       System.out.println(d);    } }OutputMon Nov 19 16:11:31 UTC 2018

Get the Last Day of Month in Java

Samual Sam
Updated on 27-Jun-2020 09:05:14

14K+ Views

The getActualMaximum() method returns the maximum value that the specified calendar field could have, based on the time value of this Calendar.Import the following package to work with Calendar class in Java, import java.util.Calendar;Create a calendar object.Calendar cal = Calendar.getInstance();Use the getActualMaximum() method to get the last day of the month.int res = cal.getActualMaximum(Calendar.DATE);The following is an example.Example Live Demoimport java.util.Calendar; public class Main {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       int res = cal.getActualMaximum(Calendar.DATE);       System.out.println("Today's Date = " + cal.getTime());       System.out.println("Last Date of ... Read More

Advertisements