Implement Switch on Enum in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:15:19

244 Views

Enum in Java contains a fixed set of constants. They can have fields, constructors and method. It enhances type safety in Java.The following is an example wherein we are implementing Switch statement on Enumeration in Java −Example Live Demopublic class Demo {    public static void main(String[] args) {       Laptop l = Laptop.Inspiron;       switch(l){          case Inspiron:          System.out.println("Laptop for home and office use!");             break;          case XPS:           System.out.println("Laptop for the ultimate experience!");             break;          case Alienware:           System.out.println("Laptop for high-performance gaming");             break;       }    } } enum Laptop {       Inspiron, XPS, Alienware; }OutputLaptop for home and office use!

Create BigInteger via String in Java

Samual Sam
Updated on 29-Jun-2020 05:12:13

182 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Firstly, set a string −String str = "268787878787687";Now, create a new object for BigInteger and pass the above string −BigInteger bInteger = new BigInteger(str);The following is an example −Example Live Demoimport java.math.BigInteger; public class Demo {    public static void main(String[] argv) throws Exception {       String str = "268787878787687";       BigInteger bInteger = new BigInteger(str);       System.out.println(bInteger);    } }Output268787878787687

Compare Dates in Java to Check If One Date Is Before Another

Samual Sam
Updated on 29-Jun-2020 05:11:29

226 Views

To compare dates if a date is before another date, use the Calendar.before() method.The Calendar.before() method returns whether this Calendar's time is before the time represented by the specified Object. First, let us set a date which is before the current dateCalendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2010); date1.set(Calendar.MONTH, 11); date1.set(Calendar.DATE, 20);Here is our current dateCalendar date = Calendar.getInstance();Now, use the after() method to compare both the dates as shown in the following exampleThe following is an exampleExample Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar date1 = Calendar.getInstance();       ... Read More

Get Day of Week for a Particular Date in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:10:49

4K+ Views

To get the day of week for a particular date in Java, use the Calendar.DAY_OF_WEEK constant.Let us set a date first.Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);Since, we have used the Calendar as well as GregorianCalendar classes, therefore at first, import the following packages.import java.util.Calendar; import java.util.GregorianCalendar;Now, we will find the day of week.int day = one.get(Calendar.DAY_OF_WEEK);The following is an exampleExample Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String[] argv) throws Exception {       Calendar one = new GregorianCalendar(2010, Calendar.JULY, 10);       int day = one.get(Calendar.DAY_OF_WEEK);       System.out.println(day);   ... Read More

Check If Two Calendar Objects Represent the Same Local Time in Java

Samual Sam
Updated on 29-Jun-2020 04:51:44

174 Views

Use the == operator to compare two calendar objects.Let us first create the first calendar object and set date −Calendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2040); date1.set(Calendar.MONTH, 10); date1.set(Calendar.DATE, 25); date1.set(Calendar.HOUR_OF_DAY, 11); date1.set(Calendar.MINUTE, 30); date1.set(Calendar.SECOND, 10);Now, the following is the second calendar object −Calendar date2 = Calendar.getInstance(); date2.set(Calendar.YEAR, 2040); date2.set(Calendar.MONTH, 10); date2.set(Calendar.DATE, 25); date2.set(Calendar.HOUR_OF_DAY, 11); date2.set(Calendar.MINUTE, 30); date2.set(Calendar.SECOND, 10);Let is now compare them using == and && operators −if(date1.get(Calendar.SECOND) == date2.get(Calendar.SECOND) && date1.get(Calendar.MINUTE) == date2.get(Calendar.MINUTE) && date1.get(Calendar.HOUR) == date2.get(Calendar.HOUR) && date1.get(Calendar.DAY_OF_YEAR) == date2.get(Calendar.DAY_OF_YEAR) && date1.get(Calendar.YEAR) == date2.get(Calendar.YEAR) ) {    System.out.println("The local time for the calendar objects is same...");    } ... Read More

Days Till End of Year in Java

karthikeya Boyini
Updated on 29-Jun-2020 04:50:15

553 Views

To get the days until the end of the year, find the difference between the total days in the current year with the total number of passed.Firstly, calculate the day of the year.Calendar calOne = Calendar.getInstance(); int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);Now, calculate the total days in the current year 2018.int year = calOne.get(Calendar.YEAR); Calendar calTwo = new GregorianCalendar(year, 11, 31); int day = calTwo.get(Calendar.DAY_OF_YEAR); System.out.println("Days in current year: "+day);Find the difference and you will get the days until the end of the year.The following is the complete example.Example Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String ... Read More

Get the Days Remaining in Current Year in Java

Samual Sam
Updated on 29-Jun-2020 04:44:06

2K+ Views

To get the days remaining in the current year, find the difference between the total days in the current year with the total number of days passed.First, calculate the day of year.Calendar calOne = Calendar.getInstance(); int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);Now, calculate the total days in the current year 2018.int year = calOne.get(Calendar.YEAR); Calendar calTwo = new GregorianCalendar(year, 11, 31); int day = calTwo.get(Calendar.DAY_OF_YEAR); System.out.println("Days in current year: "+day);The following is the final example that finds the difference between the above two to get the days remaining in the current year.ExampleLive Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static ... Read More

Convert Day of Year to Day of Month in Java

karthikeya Boyini
Updated on 29-Jun-2020 04:41:41

629 Views

Firstly, set the day of year using DAY_OF_YEAR constant.Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2018); cal.set(Calendar.DAY_OF_YEAR, 320);Now, get the day of month −int res = cal.get(Calendar.DAY_OF_MONTH);The following is an example −Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       cal.set(Calendar.YEAR, 2018);       cal.set(Calendar.DAY_OF_YEAR, 320);       System.out.println("Date = " + cal.getTime());       int res = cal.get(Calendar.DAY_OF_MONTH);       System.out.println("Day of month = " + res);    } }OutputDate = Fri Nov 16 07:54:55 UTC 2018 Day of month = 16Read More

Internal Python Object Serialization with Marshal

Nancy Den
Updated on 27-Jun-2020 15:01:53

1K+ Views

Even though marshal module in Python’s standard library provides object serialization features (similar to pickle module), it is not really useful for general purpose data persistence or transmission of Python objects through sockets etc. This module is mostly used by Python itself to support read/write operations on compiled versions of Python modules (.pyc files). The data format used by the marshal module is not compatible across Python versions (not even subversions). That’s why a compiled Python script (.pyc file) of one version most probably won’t execute on another. The marshal module is thus used for Python’s internal object serialization.Just as ... Read More

Python Standard Operators as Functions

Rishi Rathor
Updated on 27-Jun-2020 15:01:34

491 Views

In programming, operator is generally a symbol (key) predefined to perform a certain operation such as addition, subtraction, comparison etc. Python has a large set of built-in operations divided in different categories such as arithmetic, comparison, bit-wise, membership etc.The operator module in python library consists of functions corresponding to built-in operators. Names of the functions are analogous to type of corresponding operator. For example, add() function in operator module corresponds to + operator.Python’s Object class has dunder (double underscore before and after name) methods corresponding to operator symbols. These dunder methods can be suitably overloaded in user defined classes to ... Read More

Advertisements