Posts

Showing posts from April, 2021

Jarvis 0.0.1

  package com.company ; import java.time.LocalDateTime ; import java.time.format.DateTimeFormatter ; import java.util.Date ; import java.util.Scanner ; class Jarvis{ public String name ; public void date(){ LocalDateTime dt = LocalDateTime.now() ; // This is date DateTimeFormatter df = DateTimeFormatter.ofPattern( "dd/MM/yyyy" ) ; // This is date format String myDate = dt.format(df) ; //Creating date string using date and format System.out.println( "Today's date is " + myDate) ; } public void time(){ LocalDateTime dt = LocalDateTime.now() ; // This is date DateTimeFormatter df = DateTimeFormatter.ofPattern( "H:M a" ) ; // This is date format String myDate = dt.format(df) ; //Creating date string using date and format System.out.println( "It's " +myDate) ; } public void day(){ LocalDateTime dt = LocalDateTime.now() ; // This is date Date...

Using Api In Java

  package com.company ; import java.io.IOException ; import java.net.URI ; import java.net.http.HttpClient ; import java.net.http.HttpRequest ; import java.net.http.HttpResponse ; public class Main { public static void main (String[] args) throws IOException , InterruptedException { var url = "f0574529f9324dc3b291eb6a569fcbed" ; // httprequest var request = HttpRequest. newBuilder ().GET().uri(URI. create (url)).build() ; var client = HttpClient. newBuilder ().build() ; var response = client.send(request , HttpResponse.BodyHandlers. ofString ()) ; System. out .println(response.statusCode()) ; System. out .println(response.body()) ; } }

File Handling In Java

  package com.company ; import java.io.File ; import java.io.FileNotFoundException ; import java.io.FileWriter ; import java.io.IOException ; import java.util.Scanner ; public class tuts { public static void main (String[] args) { /* // Creating new File File myFile = new File("aryan111.txt"); try { myFile.createNewFile(); } catch (IOException e) { System.out.println("Unable to create this file"); } // Code to write to a file try { FileWriter fileWriter = new FileWriter("aryan111.txt"); fileWriter.write("This is our first file from this Java Course\nOk now Bye"); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } // Reading A File File myFile = new File("aryan111.txt"); try { Scanner sc = new Scanner(myFile); while(sc.h...

Creating JavaDoc With Method Tags

  package com.company ; /** * This class is to demonstrate what javadoc is and how it is used in the java industry * This is <i> italic </i> word <p> this is a new paragraph </p> * @author Aryan * @version 0.1 * @since 2002 * @see <a href="https://docs.oracle.com/en/java/javase/14/docs/api/index.html" target="_blank"> Java Docs </a> */ public class tuts { /** * @param args These are arguments supplied to the command line */ public static void main (String[] args) { System. out .println( "I am main method !!" ) ; } /** * * @param i This is the first number to add * @param j This is the second number to add * @return Sum of two numbers as integers * @exception Exception if i or j is more than 2147483647 */ public int add ( int i , int j) throws Exception { if (i== 2147483647 ) { throw new Exception() ; } if (j== 2...

Creating JavaDoc With Tags

package com.company ; /** * This class is to demonstrate what javadoc is and how it is used in the java industry * This is <i> italic </i> word <p> this is a new paragraph </p> * @author Aryan * @version 0.1 * @since 2002 * @see <a href="https://docs.oracle.com/en/java/javase/14/docs/api/index.html" target="_blank"> Java Docs </a> */ public class tuts { public void add ( int a , int b){ System. out .println( "The sum is: " + a+b) ; } public static void main (String[] args) { System. out .println( "This is my main method" ) ; } }

DateTimeFormatter in Java

Image
package com.company; import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class tuts {      public static void main(String[] args) {           LocalDateTime dt = LocalDateTime.now(); // This is date           System.out.println(dt);           DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy -- E H:M a"); // This is date format           String myDate = dt.format(df); //Creating date string using date and format           System.out.println(myDate);      } } Link For Java Doc : https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html  

java.time API - Classes & Methods

  package com.company ; import java.time.LocalDate ; import java.time.LocalTime ; public class tuts { public static void main (String[] args) { LocalDate d = LocalDate. now () ; System. out .println(d) ; LocalTime t = LocalTime. now () ; System. out .println(t) ; } }

GregorianCalendar class & TimeZone in java

  package com.company ; import java.util.Calendar ; import java.util.GregorianCalendar ; import java.util.TimeZone ; public class tuts { public static void main (String[] args) { // Calendar c = Calendar.getInstance(); // System.out.println(c.getCalendarType()); // System.out.println(c.getTimeZone()); // Calendar c = Calendar. getInstance () ; System. out .println(c.getCalendarType()) ; System. out .println(c.getTimeZone().getID()) ; System. out .println(c.get(Calendar. AM_PM )) ; System. out .println(c.get(Calendar. DATE )) ; System. out .println(c.get(Calendar. HOUR )) ; System. out .println(c.get(Calendar. HOUR_OF_DAY ) + ":" + c.get(Calendar. MINUTE ) + ":" + c.get(Calendar. SECOND )) ; GregorianCalendar cal = new GregorianCalendar() ; System. out .println(cal.isLeapYear( 2020 )) ; System. out .println(cal.isLeapYear( 2019 )) ; ...

Calendar Class In Java

package com.company ; import java.util.Calendar ; import java.util.TimeZone ; public class tuts { public static void main (String[] args) { // Calendar c = Calendar.getInstance(); // System.out.println(c.getCalendarType()); // System.out.println(c.getTimeZone()); // Calendar c = Calendar. getInstance () ; System. out .println(c.getCalendarType()) ; System. out .println(c.getTimeZone().getID()) ; System. out .println(c.get(Calendar. AM_PM )) ; System. out .println(c.get(Calendar. DATE )) ; System. out .println(c.get(Calendar. HOUR )) ; System. out .println(c.get(Calendar. HOUR_OF_DAY ) + ":" + c.get(Calendar. MINUTE ) + ":" + c.get(Calendar. SECOND )) ; } }

Date Class In Java

  package com.company ; import java.util.Date ; public class tuts { public static void main (String[] args) { // System.out.println(Long.MAX_VALUE); // System.out.println(Integer.MAX_VALUE); // System.out.println(System.currentTimeMillis()); Date d = new Date() ; System. out .println(d) ; System. out .println() ; } }

Date and Time in Java

  package com.company ; public class tuts { public static void main (String[] args) { System. out .println( "Years Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 / 3600 / 24 / 365 ) ; System. out .println( "Days Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 / 3600 / 24 ) ; System. out .println( "Hours Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 / 3600 ) ; System. out .println( "Seconds Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()/ 1000 ) ; System. out .println( "Milliseconds Passed since 1970: " ) ; System. out .println(System. currentTimeMillis ()) ; } }

Arraylist in Java

package com.company ; import java.util.ArrayList ; public class tuts { public static void main (String[] args) { ArrayList<Integer> l1 = new ArrayList<>() ; l1.add( 6 ) ; l1.add( 7 ) ; l1.add( 4 ) ; l1.add( 6 ) ; l1.add( 5 ) ; for ( int i= 0 ; i<l1.size() ; i++){ System. out .println(l1.get(i)) ; } // l1.clear(); System. out .println(l1) ; System. out .println(l1.contains( 4 )) ; System. out .println(l1.contains( 44 )) ; System. out .println(l1.contains( 6 )) ; System. out .println(l1.indexOf( 948 )) ; } }

Finally Block in Java & Why is it needed!

 package com.company; public class cwh_85_finally {     public static int greet(){         try{             int a = 50;             int b = 10;             int c = a/b;             return c;         }         catch(Exception e){             System.out.println(e);         }         finally {             System.out.println("Cleaning up resources...This is the end of this function");         }         return -1;     }     public static void main(String[] args) {         int k = greet();         System.out.println(k);         int a = 7;         int b = 9;   ...

Throw And Throws In Java

 package com.company; class NegativeRadiusException extends Exception{     @Override     public String toString() {         return "Radius cannot be negative!";     }     @Override     public String getMessage() {         return "Radius cannot be negative!";     } } public class cwh_84_throw_throws {     public static double area(int r) throws NegativeRadiusException{         if (r<0){             throw new NegativeRadiusException();         }         double result = Math.PI * r * r;         return result;     }     public static int divide(int a, int b) throws ArithmeticException{         // Made By Harry         int result = a/b;         return result;     }     ...

Nested Try Catch In Java

 package com.company; import java.util.Scanner; public class cwh_82_nested_try_catch {     public static void main(String[] args) {         int [] marks = new int[3];         marks[0] = 7;         marks[1] = 56;         marks[2] = 6;         Scanner sc = new Scanner(System.in);         boolean flag = true;         while(flag) {             System.out.println("Enter the value of index");             int ind = sc.nextInt();             try {                 System.out.println("Welcome to video no 82");                 try {                     System.out.println(marks[ind]);             ...

Try - Catch Blocks In Java

  package com.company ; public class tuts { public static void main (String[] args) { int a = 6000 ; int b = 0 ; try { int c = a/b ; System. out .println(c) ; } catch (Exception e){ System. out .println( "We can perform the division and the reason is: " ) ; System. out .println(e) ; } } }