top of page

 

// program to convert date to number //date const d1 = new Date(); console.log(d1); // converting to number const result = d1.getTime(); console.log(result);

​​

​

Run Code

​

Output

Mon Nov 09 2020 10:52:32 GMT+0545 (Nepal Time) 1604898452084

Convert Date to Number 

Program to validate an email address - Using Regex

 

 

function validateEmail(email_id)

{ const regex_pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))

@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

if (regex_pattern.test(email_id)) { console.log('The email address is valid');

} else { console.log('The email address is not valid');

} } validateEmail('abc123@gmail.com'); validateEmail('hello@com');

​

 

​

Run Code

​

Output

The email address is valid The email address is not valid

 

​

// program to loop through an object using for...in loop

const student = { name: 'John', age: 20, hobbies: ['reading', 'games', 'coding'],

};

// using for...in

for (let key in student) {

let value;

// get the value

value = student[key];

console.log(key + " - " + value);

}

​​

​

Run Code

​

Output

name - John age - 20 hobbies - ["reading", "games", "coding"]

Loop Through Object Using for...in

 

​

// program to display the date

// get local machine date time const date = new Date();

// get the date as a string const n = date.toDateString();

// get the time as a string const time = date.toLocaleTimeString();

// display date console.log('Date: ' + n); // display time console.log('Time: ' + time);

​​

​

​

Run Code

​

Output

Date: Wed Aug 26 2020 Time: 1:13:12 PM

Display Current Date

 

 

{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}

Margins

Java Program to Add two Numbers

 

 

 

First Example: Sum of two numbers

 public class AddTwoNumbers {

 

   public static void main(String[] args) {

       

      int num1 = 5, num2 = 15, sum;

      sum = num1 + num2;

 

      System.out.println("Sum of these numbers: "+sum);

   }

}

​

​

    Output:

 

    Sum of these numbers: 20

​

​

​

​

Second Example: Sum of two numbers using Scanner

 

     The scanner allows us to capture the user input so that we can get the values of both the numbers from user. The program then                 calculates the sum and displays it.

 

      Import java.util.Scanner;

      public class AddTwoNumbers2 {

 

      public static void main(String[] args) {

       

        int num1, num2, sum;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter First Number: ");

        num1 = sc.nextInt();

       

        System.out.println("Enter Second Number: ");

        num2 = sc.nextInt();

       

        sc.close();

        sum = num1 + num2;

        System.out.println("Sum of these numbers: "+sum);

        }

      }

 

​

​

      Output:

      Enter First Number:

      121

      Enter Second Number:

      19

      Sum of these numbers: 140

Basic Date Formatting

 

package com.ack.j2se.date;

 

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

public class BasicDateFormatting {

  public static void main( String[] args ) throws Exception {

    // get today's date

    Date today = Calendar.getInstance().getTime();

 

    // create a short version date formatter

    DateFormat shortFormatter

        = SimpleDateFormat.getDateInstance( SimpleDateFormat.SHORT );

 

    // create a long version date formatter

    DateFormat longFormatter

        = SimpleDateFormat.getDateInstance( SimpleDateFormat.LONG );

 

    // create date time formatter, medium for day, long for time

    DateFormat mediumFormatter

        = SimpleDateFormat.getDateTimeInstance( SimpleDateFormat.MEDIUM,

                                                SimpleDateFormat.LONG );

 

    // use the formatters to output the dates

    System.out.println( shortFormatter.format( today ) );

    System.out.println( longFormatter.format( today ) );

    System.out.println( mediumFormatter.format( today ) );

 

    // convert form date -> text, and text -> date

    String dateAsText = shortFormatter.format( today );

    Date textAsDate = shortFormatter.parse( dateAsText );

    System.out.println( textAsDate );

  }

}

bottom of page