Sunday, 22 May 2022

How to write a program to print month of the year in Java

Write a program to print month of the year in Java.

import java.util.*;
import java.time.*;
import java.time.temporal.TemporalAdjusters;

/**
 *
 * finding current month using Calendar class and LocalDate
 */
public class CurrentMonthInJava{

    public static void main(String[] args) {
                
        String[] month = {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" };
 

            Calendar c=Calendar.getInstance();
            
            System.out.println(c);
            int mon=c.get(Calendar.MONTH)+1;            
            System.out.println("The current month of year is "+month[mon-1]);
            System.out.println("The previous month of year is "+month[mon-2]);
            System.out.println("The next month of year is "+month[mon]);
            
            
             // month,year
    LocalDate today = LocalDate.now();
    LocalDate currentDate = LocalDate.parse(""+today);
 
            // Get day from date
        int day = currentDate.getDayOfMonth();
            // method 1: Get month from date using currentDate
        Month m = currentDate.getMonth();
            // method 2: Get month from date using today
        Month m1=today.getMonth();
            // Get year from date
        int year = currentDate.getYear();
 
        // Print the day, month, and year
        System.out.println("Current Day: " + day);
        System.out.println("Current Month: " + m);
        System.out.println("Current Year: " + year);
        System.out.println(m1);
            
    // Printing remaining number of months
    
     
     System.out.println("Today is: "+today);
     LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
     Period period = today.until(lastDayOfYear);    
     System.out.println("Months remaining in the year: "+period.getMonths());    
     
   
     
    }
    
}

Output:

java.util.GregorianCalendar[time=1653191579706,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=2022,MONTH=4,WEEK_OF_YEAR=22,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=142,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=22,SECOND=59,MILLISECOND=706,ZONE_OFFSET=19800000,DST_OFFSET=0]
The current month of year is May
The previous month of year is April
The next month of year is June
Current Day: 22
Current Month: MAY
Current Year: 2022
MAY
Today is: 2022-05-22
Months remaining in the year: 7

 


Sunday, 20 March 2022

How to read json file in python without pandas?

How to read json file in python?

There are several ways are there to read data from json file. JSON which stands for javascript object notation is one of the commonly used file in application development because of its advantages. Python has so many libraries to read data from files. Here we see  python program to read data from json without using pandas.

 

 

import json
try:
    with open("emp.json",'r') as jsonfile:
        reader=json.load(jsonfile)
        for i in reader.items():
            print(i)
        jsonfile.close()
except FileNotFoundError:
    print("no file exist")



read data from json


Wednesday, 29 December 2021

How to check email already exist in database in java

One of the most important thing in applications is to check email id exist or not.
Email verification existence plays a major role while the application is running.
There are so many places are there where we need to check email id is there or not.
For example during forget password recovery we first check whether email id exist or not.Here is the java program to check email id already exist in database or not.


import java.sql.*;
class EmailExistenceCheck {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String email=sc.next();
        EmailExistenceCheck.emailValidate(email);
    
    }


    public static void emailValidate(String email){
        boolean status=false;  
        try{  
            Class.forName("oracle.jdbc.driver.OracleDriver");  
            Connection con= DriverManager.getConnection  

                                                       ("jdbc:oracle:thin:@localhost:1521:xe","system","system");  
            PreparedStatement pstmt=con.prepareStatement(  
            "select * from login where email=?");  
            pstmt.setString(1,email);       
            ResultSet rs=pstmt.executeQuery();  
            status=rs.next();  
             if(status){  
                    System.out.println("email id already exist");
                }  
            else{
                     System.out.println("email id does not exist");
                }  
    }catch(Exception e){System.out.println(e);}  
}  

}


How to check email already exist in database in java using servlet.


Thursday, 17 June 2021

How can I get data from database table into combo box in Java

How can I get data in a combo box from a database in Java | Load data from database to Combo box in Java:

In this we will see how to load data from database table into combo box component of swings in java.  

 package demo;
 

import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.*;
public class RuntimeComboBoxDemo {
 
   
   public static void main(String[] args) {

      
       try{  


            Class.forName("oracle.jdbc.driver.OracleDriver");  
            Connection con=DriverManager.getConnection(  
                            "jdbc:oracle:thin:@localhost:1521:xe","system","system");  
            System.out.println("Connected to database...");
            Statement pstmt=con.createStatement();
            ResultSet rs=pstmt.executeQuery("select distinct ename from emp"); 
       
               JFrame f=new JFrame();
               Vector<String> v=new Vector<String>();
               while(rs.next()){
                  v.add(rs.getString(1));
               }
              JComboBox jcb = new JComboBox(v);
              f.setLayout(new FlowLayout());
              f.add(jcb);
              f.setSize(300, 250);
              f.setLocationRelativeTo(null);
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.setVisible(true);
              }
    catch(Exception e){
        System.out.println(e);
    }
   }
    
}

 

 


Wednesday, 12 May 2021

What is the difference between Combo box and radio button

Difference between Combo box and radio button in java:


  • Combo box is editable. 
    Radio button is non editable.
  • Combo box take less space on screen if multiple values are there.
    Radio button take more space on screen if multiple values are there.
  • Combo box allows us to select from list.
    Radio button allows us to select from group.
  • JComboBox jcbox = new JComboBox();    
    JRadioButton option1 = new JRadioButton("");
  • For Combo box import package is, import javax.swing.JComboBox;
    For radio button import package is, import javax.swing.JRadioButton;



Sunday, 9 May 2021

Combo box in java

What is Combo box in java:

One of the Swing component is JComboxBox which has a drop-down list of choices for user that lets him to selects one of the item from the list.

It allows user to select an item from the list.The combo box can be read only or it can be editable.

The read only combo box is the one from where user can select only one value from the list  (whether the desired value is not there or not).

The editable combo box is the one from where user can select only one value from the list if the desired value is not there then he can enter his own value.

For Combo box import package is, import javax.swing.JComboBox;


Wednesday, 7 April 2021

The python program to read string and print longest word

Write a program in python to read string and print longest word and its position:

word=input('enter a value')
words=word.split()
print(words)
length=len(words)
maximum=0
pos=0
for i in range(length):
    l=len(words[i])
    if l>maximum:
        maximum=l
        pos=i   
print('longest word is',words[pos])
print('Number of characters in longest word  is',maximum)
print('Position on longest word is ',pos+1)