Advertisement

Udemy WW Udemy WW
Showing posts with label Basic Program. Show all posts
Showing posts with label Basic Program. Show all posts

Thursday, 3 September 2020

Java code to find last day of the month

java 8
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;


LocalDate date =  LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());

System.out.println("Last day of the month:"+date);

Java code to find first day of the month

 

 Calendar cal = Calendar.getInstance();  

 cal.set(Calendar.DAY_OF_MONTH, 1);

 System.out.println("First day of the current month:-"+c.getTime());



Java -8 (using java.time)


LocalDate currentDate = LocalDate.now();

System.out.println("First day of the current month:-" + currentDate.withDayOfMonth(1));

Monday, 31 August 2020

Java code to find duplicate Characters in a String

import java.util.HashMap; import java.util.Map; public class FindDuplicateChar { public static void countDuplicate(String str){ /*Create a Map to store each Character with their count */ Map<Character, Integer> charMap = new HashMap<Character, Integer>(); /* Get Character array from String */ char[] charArray = str.toCharArray(); /*Iterate over Char Array and populate map*/ for(Character ch:charArray){ if(charMap.containsKey(ch)){ charMap.put(ch, charMap.get(ch)+1); } else { charMap.put(ch, 1); } } /* Print Character with their count */ for (Map.Entry entry:charMap.entrySet()) { System.out.println(entry.getKey()+" = "+entry.getValue()); } } public static void main(String a[]){ FindDuplicateChar obj = new FindDuplicateChar(); String text = "ABCDABCDEFGHIJKLABCD"; System.out.println("Text -"+text); FindDuplicateChar.countDuplicate(text); } }

Sunday, 23 August 2020

Find no of years in between two given dates

  public static int findYearsInBetween(Date startDate, Date endDate){
   Calendar startCal = getCalendar(startDate);
        Calendar endCal = getCalendar(endDate);
        int diff = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
        if (startCal.get(Calendar.MONTH) > endCal.get(Calendar.MONTH) ||
                (startCal.get(Calendar.MONTH) == endCal.get(Calendar.MONTH
&& startCal.get(Calendar.DATE) > endCal.get(Calendar.DATE))) {
            diff--;
        }
        return diff;
}

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH); String startDateString = <Give start date here>;
String endDateString = <Give end date here>; Date startDate = formatter.parse(startDateString);
Date endDate = formatter.parse(endDateString);

System.out.print("No of days :"+ findYearsInBetween(startDate,endDate));

Thursday, 8 December 2011

Socket Programming in java( Simple Chat Program)

Program For Server
import java.io.*;
import java.net.*;

public class MyServer
{
ServerSocket ss;
Socket s;
DataInputStream din;
DataOutputStream dout;
public MyServer()
{
try{

System.out.println("Server START......");
ss=new ServerSocket(8);
s=ss.accept();
System.out.println("Client Connected.....");
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
chat();
}catch(Exception e){System.out.println(e);}
}

public void chat()throws IOException
{
String str=" ";
do{
str=din.readUTF();
System.out.println("Client Message: "+str);
dout.writeUTF("I have recieved ur message:"+str);
dout.flush();

}while(!str.equals("stop"));

}

public static void main(String arg[])
{
new MyServer();
}

}

Open console and Compile it and run it
C:\java MyServer

Client Program

import java.io.*;
import java.net.*;

public class MyClient
{
Socket s;
DataInputStream din;
DataOutputStream dout;
public MyClient()
{
try{
s=new Socket("127.0.0.1",8);
System.out.println(s);
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
chat();
}catch(Exception e){System.out.println(e);}
}

public void chat()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1;
do{
s1=br.readLine();
dout.writeUTF(s1);
dout.flush();
System.out.println("Server Message: "+din.readUTF());
}while(!s1.equals("stop"));
}

public static void main(String arg[])
{
new MyClient();
}

}


Now open a new console window and compile it an execute as follows

c:\java MyClient
hello
start your chatting now

Tuesday, 29 November 2011

How to convert a String into Arithmetic Expression

 import bsh.*;
 class Operation
 {
    public static void main(String[] args)throws Exception
    {
        Interpreter i=new Interpreter();
        int r=(Integer)i.eval(args[0]);
        System.out.println(r);
    }
}
you just need to compile above code and interpret as follows
C:\java Operaraion 1+2+\4*6

Advertisement

Udemy WW