LearnJavaInterview Questions(Data Type And Date and Time)


 


DATA TYPE 


AddAllDigitsInTheIntegerBetween0And1000
 
import java.util.Scanner;
public class AddAllDigitsInTheIntegerBetween0And1000 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int firstDigit=a%10;
        int rem=a/10;
        int secondDigit=rem%10;
        rem=rem/10;
        int thirdDigit=rem%10;
        rem=rem/10;
        int fourthDigit=rem%10;
        int sum=thirdDigit+secondDigit+firstDigit+fourthDigit;
        System.out.println("the sum of all digits in:"+a+" is "+sum);

    }

}

FahrenheitToCelsiusDegree

import java.util.Scanner;

public class FahrenheitToCelsiusDegree {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double far=sc.nextDouble();
double cel=((5*(far-32.0))/9.0);
System.out.println(far+" degree far is equal to "+cel+" in cel");
    }

}


InchesToMeters

import java.util.Scanner;

public class InchesToMeters {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double inch=sc.nextDouble();
double meters=inch*0.0254;
System.out.println(inch+" inch is "+meters+" meters");
    }

}



PrintSumDiffrenceProductAverageDistanceMaximunMinimun

import java.util.Scanner;

public class PrintSumDiffrenceProductAverageDistanceMaximunMinimun {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("the sum is:"+(a+b));
System.out.println("the diffrences is:"+(a-b));
System.out.println("the product is:"+(a*b));
System.out.println("the average is:"+(double)(a+b)/2);
System.out.println("the distances is:"+Math.abs(-b));
System.out.println("the max is:"+Math.max(a, b));
System.out.println("the min is:"+Math.min(a,b));
    }

}


PrintTheNumberOfYearsAndDaysForTheMinutes

import java.util.Scanner;

public class PrintTheNumberOfYearsAndDaysForTheMinutes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double a=sc.nextDouble();
double mintesYear=60*24*365;
long years=(long)(a/mintesYear);
int days=(int)(a/60/24)%365;
System.out.println((int)a+" minutes is approximately"+years+" years and"+days+" days");
    }

}



DATE AND TIME


AddSomeHoursToTheCurrentTime

import java.time.LocalTime;

public class AddSomeHoursToTheCurrentTime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
LocalTime time1=LocalTime.now();
LocalTime time2=time1.plusHours(4);
System.out.println("after the 4 hours of additional is:"+time2);
    }

}


CheckYearIsLeapYearOrNot

import java.util.Scanner;

public class CheckYearIsLeapYearOrNot {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if((a%400==0)||(a%4==0 && a%100!=0)) {
    System.out.println("the year is leap year:"+a);
}
else {
    System.out.println("the year is not leap year:"+a);
}
    }

}



CreateDateObject

import java.util.Calendar;

public class CreateDateObject {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
int year=2023;
int month=1;//feburauary
int date=1;
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
System.out.println(cal.getTime());
    }

}



DateBeforeAndAfter1YearCompareToCurrentDate

import java.util.Calendar;
import java.util.Date;

public class DateBeforeAndAfter1YearCompareToCurrentDate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
Date currentdate=cal.getTime();
cal.add(Calendar.YEAR, 1);
Date nextyear=cal.getTime();
cal.add(Calendar.YEAR, -2);
Date previousyear=cal.getTime();
System.out.println("the current date is:"+currentdate);
System.out.println("the next year currentdate is:"+nextyear);
System.out.println("the previous year currentdate is:"+previousyear);
    }

}


DisplayInformationOfCalendarYearMonthDateHoursMinute

import java.util.Calendar;

public class DisplayInformationOfCalendarYearMonthDateHoursMinute {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
System.out.println("the year-month-date:HH:MM:ss is:");
    System.out.println("year is:"+cal.get(Calendar.YEAR));
    System.out.println("month is:"+cal.get(Calendar.MONTH));
    System.out.println("date is:"+cal.get(Calendar.DATE));
    System.out.println("hour is:"+cal.get(Calendar.HOUR));
    System.out.println("minute is:"+cal.get(Calendar.MINUTE));
    System.out.println("second is:"+cal.get(Calendar.SECOND));
    }

}


FindCurrentYearAndLeapYearOrNotAndNoDays

import java.time.Year;

public class FindCurrentYearAndLeapYearOrNotAndNoDays {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Year yr=Year.now();
System.out.println("the current year is:"+yr);
boolean isleap=yr.isLeap();
System.out.println("is that leap year:"+isleap);
int len=yr.length();
System.out.println("number of days in the year is:"+len);
    }

}

FindYourAge

import java.time.LocalDate;
import java.time.Period;

public class FindYourAge {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
LocalDate bdate=LocalDate.of(2002, 03, 01);//DOB
LocalDate now=LocalDate.now();//current date
Period diff=Period.between(bdate,now);
System.out.printf("\n i am %d years, %d months and %d days old.\n\n",
diff.getYears(),diff.getMonths(),diff.getDays());
    }

}


GetADateAfter2Weeks

import java.util.Date;
import java.util.Calendar;
import java.util.*;

public class GetADateAfter2Weeks {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
int nodays=14;//2 weeks 14 days
Calendar cal=Calendar.getInstance();
Date cdate= cal.getTime();
cal.add(Calendar.DAY_OF_YEAR, nodays);
Date date= cal.getTime();
System.out.println("current date is:"+cdate);
System.out.println("day after two weeks:"+date);

    }

}

GetADayOfTheWeekOfASpecificDate

import java.util.Calendar;

public class GetADayOfTheWeekOfASpecificDate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
int daysof=cal.get(Calendar.DAY_OF_WEEK);
System.out.println("the daysof the week is:"+daysof);
    }

}


GetCurrentFullDateAndTime

import java.util.Calendar;

public class GetCurrentFullDateAndTime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
System.out.println("the current date and time is:"+(cal.get(Calendar.MONTH)+1)+":"+
cal.get(Calendar.YEAR)+":"+cal.get(Calendar.DATE)+":"+cal.get(Calendar.HOUR)+":"+
cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND)+":"+
cal.get(Calendar.MILLISECOND));
    }

}

GetCurrentLocalTime

import java.time.LocalTime;

public class GetCurrentLocalTime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
LocalTime time=LocalTime.now();
System.out.println("the local time is:"+time);
    }

}


GetCurrentTimeStamp

import java.time.Instant;

public class GetCurrentTimeStamp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Instant timestamp=Instant.now();
System.out.println("the timestamp is:"+timestamp);
    }

}


GeTheDates10DaysBeforeAndAfterToday

import java.time.LocalDate;

public class GeTheDates10DaysBeforeAndAfterToday {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
LocalDate today=LocalDate.now();
System.out.println("the current date is:"+today);
System.out.println("the 10 days before the date is:"+today.plusDays(-10));
System.out.println("the 10 days after the date is:"+today.plusDays(10));

    }

}


GetLastDayOfTheCurrentMonth

import java.util.Calendar;

public class GetLastDayOfTheCurrentMonth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
System.out.println("the last day of the current month is:"+
cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    }

}


GetLasteDateOfTheMonth

import java.util.Calendar;

public class GetLasteDateOfTheMonth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println(cal.getTime());
    }

}

GetMaximumValueOfYearMonthWeekDate

import java.util.Calendar;

public class GetMaximumValueOfYearMonthWeekDate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
System.out.println("the year is:"+cal.getTime());
int maxyear=cal.getActualMaximum(Calendar.YEAR);
int maxmonth=cal.getActualMaximum(Calendar.MONTH);
int maxweek=cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
int maxdate=cal.getActualMaximum(Calendar.DATE);

System.out.println("the maximum year is:"+maxyear);
System.out.println("the maximum month is:"+maxmonth);
System.out.println("the maximum week is:"+maxweek);
System.out.println("the maximum date is:"+maxdate);
    }

}


GetMinimumValueOfAnYearMonthWeekDate

import java.util.Calendar;

public class GetMinimumValueOfAnYearMonthWeekDate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Calendar cal=Calendar.getInstance();
        System.out.println("the year is:"+cal.getTime());
        int minyear=cal.getActualMinimum(Calendar.YEAR);
        int minmonth=cal.getActualMinimum(Calendar.MONTH);
        int minweek=cal.getActualMinimum(Calendar.WEEK_OF_YEAR);
        int mindate=cal.getActualMinimum(Calendar.DATE);

        System.out.println("the minimum year is:"+minyear);
        System.out.println("the minimum month is:"+minmonth);
        System.out.println("the minimum week is:"+minweek);
        System.out.println("the minimum date is:"+mindate);
    }

}

GetNameOfFirstAndLastDayOfMonth

import java.time.YearMonth;
import java.util.Calendar;

public class GetNameOfFirstAndLastDayOfMonth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
YearMonth ym=YearMonth.of(2023, 2);
String  firstday=ym.atDay(1).getDayOfWeek().name();
String  lastday=ym.atEndOfMonth().getDayOfWeek().name();
System.out.println("the firstday of the month is:"+firstday);
System.out.println("the lastday of the month is:"+lastday);

    }

}

GetNewyorkTime

import java.util.Calendar;
import java.util.TimeZone;

public class GetNewyorkTime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("America/NewYork"));
System.out.println();
System.out.println("Time in NewYork:"+cal.get(Calendar.HOUR_OF_DAY)+":"+
cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
    }

}


GetTheCurrentTimeInAllTheAvailableTimeZones

import java.time.LocalDateTime;
import java.time.ZoneId;

public class GetTheCurrentTimeInAllTheAvailableTimeZones {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
ZoneId.SHORT_IDS.keySet().stream().forEach(zoneKey->System.out.println(" "+
ZoneId.of(ZoneId.SHORT_IDS.get(zoneKey))+": "+
LocalDateTime.now(ZoneId.of(ZoneId.SHORT_IDS.get(zoneKey)))));
    }

}

GetTheInformationOfTime

import java.time.LocalTime;

public class GetTheInformationOfTime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
LocalTime lc=LocalTime.now();
int hour=lc.getHour();
int min=lc.getMinute();
int sec=lc.getSecond();
int nanosec=lc.getNano();
System.out.println("the hour is:"+hour);
System.out.println("the min is:"+min);
System.out.println("the sec is:"+sec);
System.out.println("the nanosec is:"+nanosec);
    }

}


GetTheMonthsRemainingInTheYear


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

public class GetTheMonthsRemainingInTheYear {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
LocalDate today=LocalDate.now();
LocalDate lastDayOfYear=today.with(TemporalAdjusters.lastDayOfYear());
Period period=today.until(lastDayOfYear);
System.out.println("Months remaing in the year :"+period.getMonths());
    }

}


GetTheNumberOfDaysOfAMonth

import java.util.Calendar;

public class GetTheNumberOfDaysOfAMonth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Calendar cal=Calendar.getInstance();
int days=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("the days of the month is:"+days);
    }

}

NumberOfDaysBetweenTwoGivenYears

import java.util.Scanner;

public class NumberOfDaysBetweenTwoGivenYears {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
if(b>a) {
    for(int i=a;i<=b;i++) {
        System.out.println("year :"+i+"="+nodays(i));
    }
}else {
    System.out.println("end year must be greater than first year!");
}
    }
    public static int nodays(int year) {
        if(is_leap_year(year))
            return 366;
        else
            return 365;
    }
public static boolean is_leap_year(int year) {
    return (year%400==0)||year%4==0&&year%100!=0;
    }
}



















Post a Comment

Previous Post Next Post