Convert IST to GMT

 import java.util.*;


public class TimezoneConvertor {

    public static void main(String[] args) {
       
        Scanner sc = new Scanner(System.in);

        // Taking user input for minute, hour, day, month, and year
        int min = sc.nextInt();
        int hour = sc.nextInt();
        int day = sc.nextInt();
        int month = sc.nextInt();
        int year = sc.nextInt();
       
        // Close scanner to avoid resource leaks
        sc.close();

        // Adjust hours and minutes for IST to GMT conversion
        hour = hour + 5;
        min = min + 30;

        if (min >= 60) {
            hour = hour + 1;
            min = min - 60;
        }
        if (hour >= 24) {
            day = day + 1;
            hour = hour - 24;
        }

        // Define month lengths (adjust February based on leap year)
        int[] monthLengths = {31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        // Adjust for days overflowing month length
        if (day > monthLengths[month - 1]) {
            day -= monthLengths[month - 1];
            month++;
        }

        // Adjust for months overflowing year
        if (month > 12) {
            month = month - 12;
            year++;
        }

        // Display the converted time in GMT
        System.out.println("year=" + year + " month=" + month + " day=" + day + " hour=" + hour + " min=" + min);
    }

    // Method to determine if a year is a leap year
    public static boolean isLeapYear(int year) {
        // A year is a leap year if it is divisible by 4 but not by 100, except if it's also divisible by 400.
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}

Comments

Popular posts from this blog

Convert first letter to capital letter in java

Stair case traversal search

Palindrome of string my logic