Skip to content

Instantly share code, notes, and snippets.

@prokash-sarkar
Last active January 12, 2016 04:28
Show Gist options
  • Save prokash-sarkar/4571f9f9240d78c50e8e to your computer and use it in GitHub Desktop.
Save prokash-sarkar/4571f9f9240d78c50e8e to your computer and use it in GitHub Desktop.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class HelloWorld {
public static void main(String args[]) throws ParseException {
System.out.print("\n" + "# 1 (SAT/SUN) " + getValidity("Sat", "Sun", "19:00:00", "21:00:00") + "\n");
System.out.print("\n" + "# 2 (SUN/MON) " + getValidity("Sun", "Mon", "17:00:00", "18:00:00") + "\n");
System.out.print("\n" + "# 3 (MON/TUE) " + getValidity("Mon", "Tue", "17:00:00", "18:00:00") + "\n");
System.out.print("\n" + "# 4 (TUE/WED) " + getValidity("Tue", "Wed", "17:00:00", "18:00:00") + "\n");
System.out.print("\n" + "# 5 (WED/THU) " + getValidity("Wed", "Thu", "17:00:00", "18:00:00") + "\n");
System.out.print("\n" + "# 6 (THU/FRI) " + getValidity("Thu", "Fri", "17:00:00", "18:00:00") + "\n");
System.out.print("\n" + "# 7 (FRI/SAT) " + getValidity("Fri", "Sat", "17:00:00", "18:00:00") + "\n");
System.out.print("\n" + "# 8 (SAT/SAT) " + getValidity("Sat", "Sat", "20:00:00", "20:00:00") + "\n");
}
/**
* Process the current, start & end date/time validation
*
* @param start_date
* @param end_date
* @param start_time
* @param end_time
* @return
* @throws ParseException
*/
public static boolean getValidity(String start_day, String end_day, String begin_time, String end_time)
throws ParseException {
final DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal_current = Calendar.getInstance();
Calendar cal_start = Calendar.getInstance();
Calendar cal_end = Calendar.getInstance();
// Todo: set a static value to current day and set it in the calender object
cal_current.set(Calendar.DAY_OF_WEEK, getDayCount("Sat"));
// get the dates from day name
// start_date
cal_start.set(Calendar.DAY_OF_WEEK, getDayCount(start_day));
// end_date
cal_end.set(Calendar.DAY_OF_WEEK, getDayCount(end_day));
// initialize the date objects
//Date current_date = formatter.parse(formatter.format(cal_current.getTime()));
Date temp_current_date = formatter.parse(formatter.format(cal_current.getTime()));
Date start_date = formatter.parse(formatter.format(cal_start.getTime()));
Date temp_end_date = formatter.parse(formatter.format(cal_end.getTime()));
// add days to date object if the end time is earlier than the start
// time
if (temp_end_date.compareTo(start_date) < 0) {
cal_end.add(Calendar.DATE, 7);
} /*else if (temp_end_date.compareTo(temp_current_date) < 0){
cal_current.add(Calendar.DATE, -7);
}*/
// final date object after adding days
Date end_date = formatter.parse(formatter.format(cal_end.getTime()));
if (end_date.compareTo(temp_current_date) < 0){
cal_current.add(Calendar.DATE, -7);
}
Date current_date = formatter.parse(formatter.format(cal_current.getTime()));
System.out.println("Current Date: " + current_date);
System.out.println("Start Date: " + start_date);
System.out.println("End Date: " + end_date);
if ((current_date.after(start_date) || current_date.equals(start_date))
&& (current_date.before(end_date) || current_date.equals(end_date))) {
try {
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
Date today = Calendar.getInstance().getTime();
String datePresent = format.format(today);
Date dStart = new Date();
Date dStop = new Date();
Date dPresent = new Date();
dStart = format.parse(begin_time);
dStop = format.parse(end_time);
dPresent = format.parse(datePresent);
// convert the time into seconds for comparing
long current_time = TimeUnit.MILLISECONDS.toSeconds(dPresent.getTime());
long start_time = TimeUnit.MILLISECONDS.toSeconds(dStart.getTime());
long stop_time = TimeUnit.MILLISECONDS.toSeconds(dStop.getTime());
if (current_date.equals(start_date)) {
if (current_time >= start_time && current_time <= stop_time) {
return true;
} else {
return false;
}
} else {
if (current_time <= stop_time) {
return true;
} else {
return false;
}
}
// Todo: date validation without time
// return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/**
* Get the appropriate day's Integer value from a day name
*
* @param calender
* @param day
* @return
*/
public static int getDayCount(String day) {
int count = 1;
if (day.matches("Sun")) {
count = Calendar.SUNDAY;
} else if (day.matches("Mon")) {
count = Calendar.MONDAY;
} else if (day.matches("Tue")) {
count = Calendar.TUESDAY;
} else if (day.matches("Wed")) {
count = Calendar.WEDNESDAY;
} else if (day.matches("Thu")) {
count = Calendar.THURSDAY;
} else if (day.matches("Fri")) {
count = Calendar.FRIDAY;
} else if (day.matches("Sat")) {
count = Calendar.SATURDAY;
}
return count;
}
/**
* Convert a given time to GMT+8
*
* @param time
* @return
*/
public String toGMT(String time) {
// first convert the received string to date
Date date = new Date();
// creating DateFormat for converting time from local timezone to GMT
DateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
// getting GMT timezone
format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
return format.format(date).toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment