Skip to content

Commit 6cfa07b

Browse files
Add files via upload
1 parent 41a0d1b commit 6cfa07b

35 files changed

+2397
-142
lines changed

AddTimes.java

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package date;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
*
7+
* @author Rohit Agarwal
8+
* @category Date and Time
9+
* @problem How to add two given times in java?
10+
*
11+
*/
12+
public class AddTimes {
13+
14+
public static void main(String[] args) {
15+
16+
Scanner input = null;
17+
try {
18+
input = new Scanner(System.in);
19+
System.out.println("Enter first time(24 hour) in HH:mm:ss format");
20+
String time1 = input.next();
21+
System.out.println("Enter second time(24 hour) in HH:mm:ss format");
22+
String time2 = input.next();
23+
if (isTimeValid(time1) && isTimeValid(time2)) {
24+
25+
// Separating first String using delimiter ":"
26+
String[] firstTimeParts = time1.split(":");
27+
// Converting String to Integer
28+
int hours1 = Integer.parseInt(firstTimeParts[0]);
29+
int minutes1 = Integer.parseInt(firstTimeParts[1]);
30+
int seconds1 = Integer.parseInt(firstTimeParts[2]);
31+
32+
// Separating second String using delimiter ":"
33+
String[] secondTimeParts = time2.split(":");
34+
// Converting String to Integer
35+
int hours2 = Integer.parseInt(secondTimeParts[0]);
36+
int minutes2 = Integer.parseInt(secondTimeParts[1]);
37+
int seconds2 = Integer.parseInt(secondTimeParts[2]);
38+
39+
int hours = hours1 + hours2;
40+
int minutes = minutes1 + minutes2;
41+
int seconds = seconds1 + seconds2;
42+
int days = 0;
43+
44+
/**
45+
*
46+
* 60 seconds=1 minute. So if value of seconds>59 adding 1
47+
* minute to minutes. 60 minutes=1 hour So if value of
48+
* minutes>59 adding 1 hour to hours. 24 hours=1 day So if value
49+
* of hours>23 adding 1 day to days.
50+
*
51+
*/
52+
if (seconds > 59) {
53+
seconds = seconds - 60;
54+
minutes = minutes + 1;
55+
if (minutes > 59) {
56+
minutes = minutes - 60;
57+
hours = hours + 1;
58+
if (hours > 23) {
59+
hours = hours - 24;
60+
days = days + 1;
61+
}
62+
} else {
63+
64+
if (hours > 23) {
65+
hours = hours - 24;
66+
days = days + 1;
67+
}
68+
69+
}
70+
} else {
71+
if (minutes > 59) {
72+
minutes = minutes - 60;
73+
hours = hours + 1;
74+
if (hours > 23) {
75+
hours = hours - 24;
76+
days = days + 1;
77+
}
78+
} else {
79+
80+
if (hours > 23) {
81+
hours = hours - 24;
82+
days = days + 1;
83+
}
84+
85+
}
86+
}
87+
88+
// Converting each integer value of String and combining all Strings.
89+
String finalTime = String.valueOf(days) + ":" + String.valueOf(hours) + ":" + String.valueOf(minutes)
90+
+ ":" + String.valueOf(seconds);
91+
System.out.println("New time is :\n" + finalTime);
92+
System.out.println("OR");
93+
System.out.println(days + " Days " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds ");
94+
95+
} else {
96+
System.out.println("Time is not valid.");
97+
}
98+
} finally {
99+
if (input != null) {
100+
input.close();
101+
}
102+
}
103+
104+
}
105+
106+
private static boolean isTimeValid(String time) {
107+
108+
boolean result = false;
109+
/*
110+
* Regular expression that matches String with format HH:mm:ss
111+
* HH -> 0-23
112+
* mm -> 0-59
113+
* ss -> 0-59
114+
*/
115+
String pattern = "(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9]):(0?[0-9]|[1-5][0-9])";
116+
if (time.matches(pattern)) {
117+
result = true;
118+
}
119+
return result;
120+
}
121+
122+
}

AdditionInDate.java

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package date;
2+
3+
import java.text.DateFormat;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Calendar;
7+
import java.util.Date;
8+
import java.util.Scanner;
9+
10+
/**
11+
*
12+
* @author Rohit Agarwal
13+
* @category Date and Time
14+
* @problem Addition in given Date
15+
*
16+
*/
17+
public class AdditionInDate {
18+
19+
public static final String NEW_DATE_PATTERN = "MMM. dd yyyy, EEEE";
20+
21+
public static void main(String[] args) {
22+
23+
Scanner input = null;
24+
try {
25+
input = new Scanner(System.in);
26+
System.out.println("Enter date in dd/MM/yyyy format :");
27+
String date = input.next();
28+
// If Date is valid, converting String to date.
29+
Date mydate = getValidDate(date);
30+
if (mydate != null) {
31+
32+
System.out.println("Enter how many years you want to add? : ");
33+
int years = input.nextInt();
34+
System.out.println("Enter how many months you want to add? : ");
35+
int months = input.nextInt();
36+
System.out.println("Enter how many weeks you want to add? : ");
37+
int weeks = input.nextInt();
38+
System.out.println("Enter how many days you want to add? : ");
39+
int days = input.nextInt();
40+
41+
// Creating Calendar class instance.
42+
Calendar calendar = Calendar.getInstance();
43+
// Converting Date to Calendar
44+
calendar.setTime(mydate);
45+
46+
// Adding years to Calendar date.
47+
calendar.add(Calendar.YEAR, years);
48+
// Adding months to Calendar date.
49+
calendar.add(Calendar.MONTH, months);
50+
// Adding weeks to Calendar date.
51+
calendar.add(Calendar.DAY_OF_WEEK_IN_MONTH, weeks);
52+
// Adding days to Calendar date.
53+
calendar.add(Calendar.DAY_OF_MONTH, days);
54+
55+
// Converting Calendar to Date.
56+
Date newDate = calendar.getTime();
57+
DateFormat format = new SimpleDateFormat(NEW_DATE_PATTERN);
58+
String myNewDate = format.format(newDate);
59+
System.out.println("New Date : " + myNewDate);
60+
61+
} else {
62+
System.out.println("Dates are invalid.");
63+
}
64+
} finally {
65+
if (input != null) {
66+
input.close();
67+
}
68+
}
69+
70+
}
71+
72+
private static Date getValidDate(String date) {
73+
74+
Date mydate = null;
75+
if (isValidDateFormat(date)) {
76+
/**
77+
* d -> Day of month
78+
* M -> Month of year
79+
* y -> Year
80+
*/
81+
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
82+
/**
83+
* By default setLenient() is true. We should make it false for
84+
* strict date validations.
85+
*
86+
* If setLenient() is true - It accepts all dates. If setLenient()
87+
* is false - It accepts only valid dates.
88+
*/
89+
dateFormat.setLenient(false);
90+
try {
91+
mydate = dateFormat.parse(date);
92+
} catch (ParseException e) {
93+
mydate = null;
94+
}
95+
}
96+
return mydate;
97+
}
98+
99+
private static boolean isValidDateFormat(String date) {
100+
101+
/**
102+
* Regular Expression that matches String with format dd/MM/yyyy.
103+
* dd -> 01-31
104+
* MM -> 01-12
105+
* yyyy -> 4 digit number
106+
*/
107+
String pattern = "(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[0-2])\\/([0-9]{4})";
108+
boolean result = false;
109+
if (date.matches(pattern)) {
110+
result = true;
111+
}
112+
return result;
113+
}
114+
115+
}

CalculateAge.java

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package date;
2+
3+
import java.text.DateFormat;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.time.Instant;
7+
import java.time.LocalDate;
8+
import java.time.Period;
9+
import java.time.ZoneId;
10+
import java.util.Date;
11+
import java.util.Scanner;
12+
13+
/**
14+
*
15+
* @author Rohit Agarwal
16+
* @category Date and Time
17+
* @problem How to calculate age?
18+
*
19+
*/
20+
public class CalculateAge {
21+
22+
public static void main(String[] args) {
23+
24+
Scanner input = null;
25+
try {
26+
input = new Scanner(System.in);
27+
System.out.println("Enter your DOB in dd/MM/yyyy format :");
28+
String dob = input.next();
29+
Date current = new Date();
30+
// If Date is valid, converting String to date.
31+
Date mydob = getValidDate(dob);
32+
if (mydob != null && mydob.before(current)) {
33+
34+
// Get default Time zone Id.
35+
ZoneId defaultZoneId = ZoneId.systemDefault();
36+
37+
// Convert Date mydob to LocalDate
38+
Instant instant1 = mydob.toInstant();
39+
LocalDate localeDateMyDob = instant1.atZone(defaultZoneId).toLocalDate();
40+
// Convert Date current to LocalDate
41+
Instant instant2 = current.toInstant();
42+
LocalDate localeDateCurrent = instant2.atZone(defaultZoneId).toLocalDate();
43+
44+
Period period = Period.between(localeDateMyDob, localeDateCurrent);
45+
System.out.println("Age : " + period.getYears() + " Years " + period.getMonths() + " Months "
46+
+ period.getDays() + " Days");
47+
48+
} else {
49+
System.out.println("DOB is invalid.");
50+
}
51+
} finally {
52+
if (input != null) {
53+
input.close();
54+
}
55+
}
56+
57+
}
58+
59+
private static Date getValidDate(String date) {
60+
61+
Date mydate = null;
62+
if (isValidDateFormat(date)) {
63+
/*
64+
* d -> Day of month M -> Month of year y -> Year
65+
*/
66+
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
67+
/*
68+
* By default setLenient() is true. We should make it false for
69+
* strict date validations.
70+
*
71+
* If setLenient() is true - It accepts all dates. If setLenient()
72+
* is false - It accepts only valid dates.
73+
*/
74+
dateFormat.setLenient(false);
75+
try {
76+
mydate = dateFormat.parse(date);
77+
} catch (ParseException e) {
78+
mydate = null;
79+
}
80+
}
81+
return mydate;
82+
}
83+
84+
private static boolean isValidDateFormat(String date) {
85+
86+
/*
87+
* Regular Expression that matches String with format dd/MM/yyyy.
88+
* dd -> 01-31
89+
* MM -> 01-12
90+
* yyyy -> 4 digit number
91+
*/
92+
String pattern = "(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[0-2])\\/([0-9]{4})";
93+
boolean result = false;
94+
if (date.matches(pattern)) {
95+
result = true;
96+
}
97+
return result;
98+
}
99+
100+
}

ChangeTimezone.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package date;
2+
3+
import java.text.DateFormat;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
import java.util.TimeZone;
8+
9+
public class ChangeTimezone {
10+
11+
public static void main(String[] args) throws ParseException {
12+
13+
//Get Current date.
14+
Date date=new Date();
15+
//Get default Time zone.
16+
TimeZone timezone=TimeZone.getDefault();
17+
System.out.println("Time Zone : "+timezone.getID() + " | "+timezone.getDisplayName() );
18+
/*
19+
* d -> Day of month
20+
* M -> Month of year
21+
* Y -> Year
22+
* h -> Hour (1-12)
23+
* m -> minute
24+
* s -> second
25+
* a -> AM/PM
26+
* z -> Time zone
27+
*/
28+
DateFormat format=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a z");
29+
String dateInString=format.format(date);
30+
System.out.println("Current date in IST : " +dateInString);
31+
timezone=TimeZone.getTimeZone("Europe/London");
32+
System.out.println("Time Zone : "+timezone.getID() + " | "+timezone.getDisplayName() );
33+
format.setTimeZone(timezone);
34+
dateInString=format.format(date);
35+
System.out.println("Current date in GMT : " +dateInString);
36+
timezone=TimeZone.getTimeZone("America/New_York");
37+
System.out.println("Time Zone : "+timezone.getID() + " | "+timezone.getDisplayName() );
38+
format.setTimeZone(timezone);
39+
dateInString=format.format(date);
40+
System.out.println("Current date in EST : " +dateInString);
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)