Java LocalDate getDayOfWeek() Method with Examples

In this article we are going to see the use of Java LocalDate class getDayOfWeek() method with suitable examples.

Java LocalDate getDayOfWeek() Method with Examples

Explanation:

This java.time.LocalDate.getDayOfWeek() method is used to get the day of the week which date is given by the user. It returns the day of the week which is from Sunday to Saturday.

Syntax:

public DayOfWeek getDayOfWeek()

Let’s see some example programs to understand it more clearly.

Approach:

  • Create an object of localDate class.
  • Then use the getDayOfWeek() method for this particular date to print the day of that week.
  • Print the final result.

Example-1

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-11");
        //Use the getDayOfWeek() method and print the result
        System.out.println("Day-of-the-week: "+date.getDayOfWeek()); 
    }
}


Output:

Day-of-the-week: WEDNESDAY

Example-2

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-29");
        //Use the getDayOfWeek() method and print the result
        System.out.println("Day-of-the-week: "+date.getDayOfWeek()); 
    }
}
Output:

Day-of-the-week: SUNDAY

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Java LocalDate getChronology() Method with Examples

In this article we are going to see the use of Java LocalDate class getChronology()  method with suitable examples.

Java LocalDate getChronology() Method with Examples

Explanation:

This java.time.LocalDate.get(TemporalField field) method is used to get the chronology of the specified date, which is the ISO calendar system. This method does not take any parameter. It returns the ISO chronology.

Syntax:

public IsoChronology getChronology()

Let’s see a program to understand it more clearly.

Approach:

  • Create an objectof LocalDate class which will hold the parsed date.
  • Then use the getChronology() method for this particular date.
  • Print the final result.

Program:

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //create an object of LocalDate and pass the parse date into it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-11");
        //print the result
      	System.out.println("Result: "+date.getChronology()); 
   	}
}

Output:

Result: ISO

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Java LocalDate from() Method with Examples

In this article we are going to see the use of Java LocalDate class from() method with suitable examples.

Java LocalDate from() Method with Examples

This java.time.LocalDate.from(TemporalAccessor temporal) method is used to obtain an instance of LocalDate from a temporal object to create a LocalDate Time. It returns the local date.

Syntax:

public static LocalDate from(TemporalAccessor temporal)

Where,

  • temporal refers to the Temporal object that to be concverted.

Let’s see an program to understand it more clearly.

Approach:

  • Create an object of localDate class.
  • By using that LocalDate class object call from() method and use ZonedDateTime.now() method to get the value.
  • Print the final LocalDate as result.

Program:

import java.time.LocalDate;
import java.time.ZonedDateTime;
public class Main
{
    public static void main(String[] args)
    {
        //create an object of localDate class
        //pass ZonedDateTime.now() in from() method.
      	LocalDate date = LocalDate.from(ZonedDateTime.now());
        //Print the result
      	System.out.println("Local date: "+date);  
   }
}
Output:

Local date: 2022-05-25

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Java LocalDate get() Method with Examples

In this article we are going to see the use of Java LocalDate class get()  method with suitable examples.

Java LocalDate get() Method with Examples

Explanation:

This java.time.LocalDate.get(TemporalField field) method is used to get the value of the specified field from this date as an integer. It returns the value for field.

Exceptions:

  • DateTimeException –it occurs when the value is outside the range of valid values for the field.
  • UnsupportedTemporalTypeException − it occurs if the field is not supported or the range of values exceeds an integer value.
  • ArithmeticException − it occurs when the numeric overflow occurs.

Syntax:

public int get(TemporalField field)

Let’s see a program to understand it more clearly.

Approach:

  • Create an object of LocalDate class.
  • Then use the get method followed by specific command to get the required result.
  • Print the final result.

Program:

import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-10");
        //print the result by mentioning the specific item
      	System.out.println("Result: "+date.get(ChronoField.DAY_OF_MONTH)); 
   }
}
Output:

Result: 10

Let’s see an instance of exception with a program.

Approach:

  • Create an objects of LocalDate class which will hold the parsed dates.
  • Here we pass an invalid date for testing.
  • Then use the get method followed by specific command to get the required result.
  • Put all those code inside the try block and in catch block to check the exception.
  • Then print the final result.

Program:

import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Main
{
    public static void main(String[] args)
    {
        try
        {
            //Create an object of LocalDate class and assign a date to it
            //here it parses the local date
            LocalDate date = LocalDate.parse("2022-02-31");
            //print the result by mentioning the specific item
      	    System.out.println("Result: "+date.get(ChronoField.DAY_OF_MONTH));
        }
        catch(Exception excp)
        {
            //print the exception as result
            System.out.println(excp);
        } 
    }
}
Output:

java.time.format.DateTimeParseException: Text '2022-02-31' could not be parsed: Invalid date 'FEBRUARY 31'

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.

Python: Open a file using “open with” statement and benefits explained with examples

Opening a file using ‘open with’ statement and benefits in Python.

In this article we will discuss about how to open a file using ‘open with’ statement, how to open multiple files in a single ‘open with’ statement and finally its benefits. So, let’s start the topic.

The need for “open with” statement :

To understand the “open with” statement we have to go through opening a file in python. For that we can make use of the open( ) function that is in-built in python

File.txt-

New File Being Read.
DONE!!
#program :

# opened a file 
fileObj = open('file.txt')
# Reading the file content into a placeholder
data = fileObj.read()
# print file content
print(data)
#close the file
fileObj.close()
Output :
New File Being Read.
DONE!!

In case the file does not exist it will throw a FileNotFoundError .

How to open a file using “open with” statement in python :

#Program :

# opened a file using open-with
with open('file.txt', "r") as fileObj:
    # Reading the file content into a placeholder
    data = fileObj.read()
    # print file content
    print(data)
# Check if file is closed
if fileObj.closed == False:
    print('File is not closed')
else:
    print('File is already closed')
New File Being Read.
DONE!!
File is closed

The with statements created an execution block that will automatically delete any object that was created in the program, in this case even if it was not closed the reader object was deleted that closed the file automatically. This saves us some memory in case we forgot to close the file.

Benefits of calling open() using “with statement” :

  • Fewer chances of bug due to coding error

With “with” statement we don’t have to close the opened file manually. It takes care of that when the compiler goes out of the block and automatically closes file. So it reduces the chances of bugs, lines of code and releases the memory for other operations.

  • Excellent handling in case of exception

If we have used “open-with” statement to open a file, and an exception occurs inside the with block, the file will be closed and the control moves to the except block.

# Python :

# Before handling the exception file will be closed 
try:
    # using "with statement" with open() function
    with open('file.txt', "r") as fileObj:
        # reading the file content
        data = fileObj.read()
        # Division by zero error
        x = 1 / 0
        print(data)
except:
    # handling the exception caused above
    print('Error occurred')
    if fileObj.closed == False:
        print('File is not closed')
    else:
        print('File is closed')
Output :
Error occurred
File is closed
  • Open multiple files in a single “with statement” :

We can use open with statement to open multiple files at the same time. Let’s try reading from one file and writing into another-

# Program :

# Read from file.txt and write in output.txt
with open('output.txt', 'w') as fileObj2, open('file.txt', 'r') as fileObj1:
    data = fileObj1.read()
    fileObj2.write(data)
    # Both the files are automatically close when the control moves out of the with block.

This will generate a “outuput.txt” file that will have the same contents as our old “file.txt”.

Output : 
Output.txt- 
New File Being Read. 
DONE !!

The files will automatically close when the control moves outside the with block.

Python: Remove characters from string by regex and 4 other ways

Removing characters from string by regex and many other ways in Python.

In this article we will get to know about deleting characters from string in python by regex() and various other functions.

Removing characters from string by regex :

sub() function of regex module in Python helps to get a new string by replacing a particular pattern in the string by a string replacement. If no pattern found, then same string will be returned.

Removing all occurrences of a character from string using regex :

Let we want to delete all occurrence of ‘a’ from a string. We can implement this by passing in sub() where it will replace all ‘s’ with empty string.

#Program :

import re
origin_string = "India is my country"

pattern = r'a'

# If found, all 'a' will be replaced by empty string
modif_string = re.sub(pattern, '', origin_string )
print(modif_string)
Output :
Indi is my country

Removing multiple characters from string using regex in python :

Let we want to delete all occurrence of ‘a’, ‘i’. So we have to replace these characters by empty string.

#Program :

import re
origin_string = "India is my country"
pattern = r'[ai]'
# If found, all 'a' & 'i' will be replaced by empty string
modif_string = re.sub(pattern, '', origin_string )
print(modif_string) 
Output :
Ind s my country

Removing characters in list from the string in python :

Let we want to delete all occurrence of ‘a’ & ‘i’ from a string and also these characters are in a list.

#Program :

import re
origin_string = "India is my country"
char_list = ['i','a']
pattern = '[' + ''.join(char_list) + ']'
# All charcaters are removed if matched by pattern
modif_string = re.sub(pattern, '', origin_string)
print(modif_string)
Output :
Ind s my country

Removing characters from string using translate() :

str class of python provide a function translate(table). The characters in the string will be replaced on the basis of mapping provided in translation table.

Removing all occurrence of a character from the string using translate() :

Let we want to delete all occurrence of ‘i’ in a string. For this we have to pass a translation table to translate() function where ‘i’ will be mapped to None.

#Program :

origin_string = "India is my country"
# If found, remove all occurence of 'i' from string
modif_string = origin_string.translate({ord('i'): None})
print(modif_string)
Output :
Inda s my country

Removing multiple characters from the string using translate() :

Let, we want to delete ‘i’, ‘y’ from the above string.

#Program :

org_string= "India is my country"
list_char = ['y', 'i']
# Remove all 's', 'a' & 'i' from the string
mod_string = org_string.translate( {ord(elem): None for elem in list_char} )
print(mod_string)
Output :
Inda s m countr

Removing characters from string using replace()  :

Python provides str class, from which the replace() returns the copy of a string by replacing all occurrence of substring by a replacement. In the above string we will try to replace all ‘i’ with ‘a’.

#Program :

origin_string = "India is my country"
# Replacing all of 's' with 'a'
modif_string = origin_string.replace('i', 'a')
print(modif_string)
Output :
Indaa as my country

Removing characters from string using join() and generator expression :

Let we have a list with some characters. For removing all characters from string that are in the list, we would iterate over each characters in the string and join them except those which are not in the list.

#Program :

origin_string= "India is my country"
list_char = ['i', 'a', 's']
# Removes all occurence of 'i', 'a','s' from the string
modif_string = ''.join((elem for elem in origin_string if elem not in list_char))
print(modif_string)
Output :
Ind  my country

Removing characters from string using join and filter() :

filter() function filter the characters from string based on logic provided in call back function. If we provide a call back function as lambda function, it checks if the characters in the list are filtered are not. After that it joins the remaining characters to from a new string i.e. it eliminates all occurrence of characters that are in the list form a string.

#Programn :

origin_string = "India is my country"
list_char = ['i', 'a', 's']
# To remove all characters in list, from the string
modif_string = ''.join(filter(lambda k: k not in list_char, origin_string))
print(modif_string)
Output :
Ind  my country

 

Pandas: Create Series from dictionary in python

Creating Series from dictionary in python

In this article we will discuss about different ways to convert a dictionary in python to a Pandas Series object.

Series class provides a constructor in Pandas i.e

Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

Where,

  • data : It represents array-like, Iterable sequence where all items in this iterable sequence will be added as values in the Series.
  • index : It represents array-like, Iterable sequence where all values in this iterable sequence will be added as indices in the Series.
  • dtype : It represents datatype of the output series.

Create a Pandas Series from dict in python :

By passing the dictionary to the Series class Constructor i.e. Series(). we can get a new Series object where all the keys in the dictionary will become the indices of the Series object, and all the values from the key-value pairs in the dictionary will converted into the values of the Series object.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 56,
    "A": 23,
    'D': 43,
    'E': 78,
    'B': 11
}
# Converting a dictionary to a Pandas Series object.
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
series_object = pd.Series(dict)
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
C  56
A  23
D  43
E  78
B  11
dtype: int64

Where the index of the series object contains the keys of the dictionary and the values of the series object contains the values of the dictionary.

Create Pandas series object from a dictionary with index in a specific order :

In the above example we observed the indices of the series object are in the same order as the keys of the dictionary. In this example we will see how to convert the dictionary into series object with some other order.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 6,
    "A": 3,
    'D': 4,
    'E': 8,
    'B': 1
}
# Creating Series from dict, but pass the index list separately
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
# But the order of indices will be some other order
series_object = pd.Series(dict,
                       index=['E', 'D', 'C', 'B', 'A'])
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
E  8
D  4
C  6
B  1
A  3
dtype: int64

Create a Pandas Series object from specific key-value pairs in a dictionary :

In above examples we saw Series object is created from all the items in the dictionary as we pass the dictionary as the only argument in the series constructor. But now we will see how we will see how we can convert specific key-value pairs from dictionary to the Series object.

So, let’s see the example.

# Program :

import pandas as pd
# Dictionary 
dict = {
    'C': 6,
    "A": 3,
    'D': 4,
    'E': 8,
    'B': 1
}
# Creating Series from dict, but pass the index list separately
# Where dictionary keys will be converted into index of Series &
# values of dictionar will become values in Series.
# But here we have passed some specific key-value pairs of dictionary
series_object = pd.Series(dict,
                       index=['E', 'D', 'C'])
print('Contents of Pandas Series: ')
print(series_object)
Output :
Contents of Pandas Series: 
E 8
D 4
C 6
dtype: int64

 

Pandas : Sort a DataFrame based on column names or row index labels using Dataframe.sort_index()

Sorting a DataFrame based on column names or row index labels using Dataframe.sort_index() in Python

In this article we will discuss how we organize the content of data entered based on column names or line reference labels using Dataframe.sort_index ().

Dataframe.sort_index():

In the Python Pandas Library, the Dataframe section provides a member sort sort_index () to edit DataFrame based on label names next to the axis i.e.

DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, by=None)

Where,

  • axis :If the axis is 0, then the data name will be sorted based on the line index labels. The default is 0
  • ascending :If the axis is 1, then the data name will be sorted based on column names.
  • inplace : If the type of truth in the rise of another type arrange in order. The default is true
  • na_position :  If True, enable localization in Dataframe. Determines NaN status after filter i.e. irst puts NaNs first, finally puts NaNs at the end.

It returns the edited data object. Also, if the location dispute is untrue then it will return a duplicate copy of the provided data, instead of replacing the original Dataframe. While, if the internal dispute is true it will cause the current file name to be edited.

Let’s understand some examples,

# Program :

import pandas as pd
# List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# Create a DataFrame object
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
print(dfObj)
Output :
    Name   Marks  City
b  Rama     31   canada
a  Symon   23   Chennai
f   Arati      16   Maharastra
e  Bhabani  32  Kolkata
d  Modi      33  Uttarpradesh
c  Heeron  39   Hyderabad

Now let’s see how we organize this DataFrame based on labels i.e. columns or line reference labels,

Sort rows of a Dataframe based on Row index labels :

Sorting by line index labels we can call sort_index() in the data name item.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting the rows of dataframe based on row index label names
modDFObj = dfObj.sort_index()
print(' Dataframes are in sorted oreder of index value given:')
print(modDFObj)
Output :
Dataframes are in sorted oreder of index value given:
    Name    Marks        City
a Symon      23         Chennai
b Rama        31         canada
c Heeron     39         Hyderabad
d Modi        33         Uttarpradesh
e Bhabani    32         Kolkata
f Arati          16         Maharastra

As we can see in the output lines it is sorted based on the reference labels now. Instead of changing the original name data backed up an edited copy of the dataframe.

Sort rows of a Dataframe in Descending Order based on Row index labels :

Sorting based on line index labels in descending order we need to pass the argument = False in sort_index() function in the data object object.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting the rows of dataframe in descending order based on row index label names
conObj = dfObj.sort_index(ascending=False)
print('The Contents of Dataframe are sorted in descending Order based on Row Index Labels are of :')
print(conObj)
The Contents of Dataframe are sorted in descending Order based on Row Index Labels are of :
     Name       Marks          City
f      Arati          16          Maharastra
e     Bhabani     32          Kolkata
d     Modi        33           Uttarpradesh
c     Heeron     39           Hyderabad
b     Rama       31           canada
a     Symon     23           Chennai

As we can see in the output lines it is sorted by destructive sequence based on the current reference labels. Also, instead of changing the original data name it restored the edited copy of the data.

Sort rows of a Dataframe based on Row index labels in Place :

Filtering a local data name instead of finding the default copy transfer inplace = True in sort_index () function in the data object object to filter the data name with local reference label labels i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
#By sorting the rows of dataframe in Place based on row index label names
dfObj.sort_index(inplace=True)
print('The Contents of Dataframe are sorted in Place based on Row Index Labels are of :')
print(dfObj)
Output :
The Contents of Dataframe are sorted in Place based on Row Index Labels are of :
     Name     Marks      City
a    Symon     23       Chennai
b     Rama     31        canada
c   Heeron     39     Hyderabad
d     Modi     33     Uttarpradesh
e  Bhabani     32       Kolkata
f    Arati       16       Maharastra

Sort Columns of a Dataframe based on Column Names :

To edit DataFrame based on column names we can say sort_index () in a DataFrame object with an axis= 1 i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting a dataframe based on column names
conObj = dfObj.sort_index(axis=1)
print('The Contents are of Dataframe sorted based on Column Names are in the type :')
print(conObj)

Output :
The Contents are of Dataframe sorted based on Column Names are in the type :
           City          Marks     Name
b        canada         31      Rama
a       Chennai         23     Symon
f      Maharastra     16     Arati
e       Kolkata          32     Bhabani
d  Uttarpradesh     33     Modi
c     Hyderabad     39      Heeron

As we can see, instead of changing the original data name it returns a fixed copy of the data data based on the column names.

Sort Columns of a Dataframe in Descending Order based on Column Names :

By sorting DataFrame based on column names in descending order, we can call sort_index () in the DataFrame item with axis = 1 and ascending = False i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting a dataframe in descending order based on column names
conObj = dfObj.sort_index(ascending=False, axis=1)
print('The Contents of Dataframe sorted in Descending Order based on Column Names are of :')
print(conObj)
Output :
The Contents of Dataframe sorted in Descending Order based on Column Names are of :
Name  Marks          City
b     Rama     31        canada
a    Symon     23       Chennai
f    Arati     16    Maharastra
e  Bhabani     32       Kolkata
d     Modi     33  Uttarpradesh
c   Heeron     39     Hyderabad

Instead of changing the original data name restore the edited copy of the data based on the column names (sorted by order)

Sort Columns of a Dataframe in Place based on Column Names :

Editing a local data name instead of obtaining an approved copy pass input = True and axis = 1 in sort_index () function in the dataframe object to filter the local data name by column names i.e.

import pandas as pd
# The List of Tuples
students = [ ('Rama', 31, 'canada') ,
             ('Symon', 23, 'Chennai' ) ,
             ('Arati', 16, 'Maharastra') ,
             ('Bhabani', 32, 'Kolkata' ) ,
             ('Modi', 33, 'Uttarpradesh' ) ,
             ('Heeron', 39, 'Hyderabad' )
              ]
# To create DataFrame object 
dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c'])
# By sorting a dataframe in place based on column names
dfObj.sort_index(inplace=True, axis=1)
print('The Contents of Dataframe sorted in Place based on Column Names are of:')
print(dfObj)

Output :
The Contents of Dataframe sorted in Place based on Column Names are of:
City  Marks     Name
b        canada     31     Rama
a       Chennai     23    Symon
f    Maharastra     16    Arati
e       Kolkata     32  Bhabani
d  Uttarpradesh     33     Modi
c     Hyderabad     39   Heeron

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Modify a Dataframe

deque vs vector : What to choose ?

Deque vs Vector

In this article, we are going to see the difference between the STL sequential containers std::deque and std::vector with their appropriate usage.

VECTOR :

  • Vectors are dynamic arrays that can shrink and expand when an element is added.
  • The container handles the memory automatically.
  • The data can be inserted at the middle or at the end.
  • The elements are stored in contiguous storage.

DEQUE :

  • Deques or double-ended queues are sequence containers that shrink and expand from both the ends.
  • Data can be inserted from the start, middle and ends.
  • The data is not stored in contiguous storage locations always.

What’s the difference ?

  1. While vector is like Dynamic array. Deque is the data structure implementation of the double-ended queue.
  2. While in vector insertion and deletion at end has good performance, however insertion and deletion from middle performs badly. But deque provides the same performance like vector insertion and deletion at end for both end and middle. Also has good performance with insertion and deletion at start.
  3. While vectors store elements contiguous which makes it faster to operate at the end faster than deques. The deques are not stored contiguous but are a list of various locations where the elements are stored so overall operations at first, mid and end positions are faster than vectors as it does not have to shift elements to store an element.

Appropriate place to use :

When there are list like operations, where additions and deletion only happen at the bottom, vectors are suitable to use. In case we want to operate on the top position as well it is suitable to use deques for the purpose.

 

Java LocalDate atStartOfDay( ) Method with Example

In this article we are going to see the use of Java LocalDate class atStartOfDay( ) method with suitable examples.

Java LocalDate atStartOfDay( ) Method with Example

This java.time.LocalDate.atStartOfDay() method is used to combine the date with the time of midnight to create a LocalDate Time. It returns the local date’s time of midnight at the start of the day.

Syntax:

public LocalDateTime atStartOfDay()

Let’s see the use of atStartOfDay( ) method with 2 different format.

Method-1: without parameters

Approach:

  1. Create an object of LocalDate class which will hold the parsed date.
  2. By using that LocalDate class object call atStartOfDay() method and assign the result to an object of LocaldateTime class.
  3. Then print the final LocalDateTime as result.

Program:

import java.time.LocalDate;
import java.time.LocalDateTime;
public class Main
{
    public static void main(String[] args)
    {
    //Create an object of LocalDate class and assign a date to it
    //here it parses the local date
    LocalDate date = LocalDate.parse("2022-05-10");
    System.out.println("Specific date: "+date);  
    //Create an object of LocalDateTime
    //call the atStartOfDay() method by using object of LocalDate class
    LocalDateTime startTime = date.atStartOfDay();
    System.out.println("Start Time: "+startTime);  
   }
}
Output:

Specific date: 2022-05-10
Start Time: 2022-05-10T00:00

Method-2: with parameters

Approach:

  1. Create an object of LocalDate class which will hold the parsed date.
  2. By using that LocalDate class object call atStartOfDay() method and by passing ZoneId.systemDefault() as parameter and assign the result to an object of ZoneddateTime class.
  3. Here we pass the ZoneId which is already defined by system so that it will take a particular zone and represent that zone’s start time.
  4. Then print the final ZonedDateTime as result.

Program:

import java.time.*;
public class Main
{
    public static void main(String[] args)
    {
    //Create an object of LocalDate class and assign a date to it
    //here it parses the local date
    LocalDate date = LocalDate.parse("2022-05-10");
    System.out.println("Specific date: "+date);  
    //Create an object of LocalDateTime
    //call atStartOfDay() method by passing the ZoneId.systemDefault() as parameter
    ZonedDateTime startTime = date.atStartOfDay(ZoneId.systemDefault());
    System.out.println("Start Time: "+startTime);  
   }
}
Output:

Specific date: 2022-05-10
Start Time: 2022-05-10T00:00Z[GMT]

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.