I have a time interval - 12:00 PM to 11:59 PM.
When the user enters the time as "06:15 PM", check against that interval.
If the time falls into that interval, return true else return false.
I searched for thisin Google, but I didn't get the correct solution.
Can anyone help me to solve this issue?
String From = "12:00 PM";
String to = "11:59 PM";
String user_given_input = "06:15 PM";
You could parse it into a Date object using a SimpleDateFormatter.
After you've done that you can use fromDate.before(user_given_input_date) && toDate.after(user_given_input_date).
assuming fromDate, toDate & user_given_input_date are Date objects
Try this code, it should work.
static boolean checkTimeInterval(String s) {
String From = "12:00 PM";
String to = "11:59 PM";
SimpleDateFormat d = new SimpleDateFormat("hh:mm a");
boolean isLie = false;
try {
Date from = d.parse(From);
Date too = d.parse(to);
Date input = d.parse(s);
if (input.before(too) && input.after(from)) {
isLie = true;
}
} catch (ParseException e) {
System.out.println("Check input string format");
e.printStackTrace();
}
return isLie;
}
You may call this function as
checkTimeInterval("06:15 PM")
Related
So Im trying to print the string "Eastern Daylight Time" instead of EDT . This should be dynamic and not hardcoded. Looking into DateFormatter class did not lead me to an answer that worked.
Here was an example that allows me to format but did not lead me to my specific answer.
I am getting the date back in the following format -
2013-06-08T00:00:00-04:00
Here are somethings that I have tried -
1)
String dateString = changeFormatDateStringWithDefaultTimeZone(paymentConfirmation.getTransactionDate(),
"yyyy-MM-dd'T'HH:mm:ssZ",
"M/d/yyyy hh:mm a zz");
public static String changeFormatDateStringWithDefaultTimeZone(String value, String ip_format, String op_format) {
if (value == null)
return null;
try {
SimpleDateFormat opSDF = new SimpleDateFormat(op_format, Locale.US);
opSDF.setTimeZone(TimeZone.getDefault());
SimpleDateFormat inSDF = new SimpleDateFormat(ip_format, Locale.US);
Date date = inSDF.parse(value);
return(opSDF.format(date));
} catch (Exception e) {
Log.e("Err", "Failed to convert time "+value);
e.printStackTrace();
}
return null;
}
2)
Date today = Calendar.getInstance().getTime();
String todayString = DateUtils.convertDateToStringWithTimeZone(today);
public static String convertDateToStringWithTimeZone(Date date){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = df.format(date);
dateString += " " + TimeZone.getDefault().getDisplayName(false, TimeZone.LONG);
return dateString;
}
These always print timezone as EDT and I want the string Eastern Daylight Time. Can anyone help me out with this?
Okay, based on your last edit of the question, the solution should be like this:
case 1)
The output pattern should be changed to "M/d/yyyy hh:mm a zzzz" (note the count of z-symbols to enforce the full zone name). Depending on the date and the underlying timezone, the formatter SimpleDateFormat will automatically determine if the daylight or the standard name is to be used.
case 2)
Use TimeZone.getDefault().getDisplayName(true, TimeZone.LONG) to enforce the long daylight name. If your default timezone is "America/New_York" then such an expression should print "Eastern Daylight Time". Note that the boolean parameter has been changed to true.
Im trying to calculate the difference between a time returned to me by an API and the current device time.
The API returns time in this format: "game_start": "2015-09-03 19:00:00"
To calculate the difference I do this:
protected String getTimeToStart(JSONObject jsonObject) {
String time = "";
Calendar startTime;
Calendar current;
startTime = Calendar.getInstance();
current = Calendar.getInstance();
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("HH mm ");
startTime.setTime(format.parse(jsonObject.getJSONObject("data").getJSONObject("game").getString("game_start")));
String[] splited = (format2.format(startTime.getTimeInMillis() - current.getTimeInMillis()).split("\\s+"));
time = splited[0] + "hrs " + splited[1] + "mins";
} catch (ParseException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
if (checkStarted(startTime, current)) {
return time;
} else {
return "League Started";
}
protected boolean checkStarted(Calendar start, Calendar current) {
if (current.before(start)) {
return true;
} else {
return false;
}
}
The problem I have is that the calculation always returns the time remaining as an hour more that it should be, until it gets to the time that it is actually meant to start then it returns that there is no time remaining e.g.
current time = 16:59
time it starts at = 17:00
time remaining that is returned 1hrs 1mins
then
current time = 17:00
time it starts at = 17:00
time remaining that is returned League Started
When parsing date from text you have to take into account the locale and the timezone. See my answer here: Date not printing correct Time Android
I want to Date comparison between Start Date and End Date. End Date is Greater than Start Date OR End Date is Equal to Start Date. How to possible? Its Comparison between only day. If I am enter Start Date is "15-12-2014" and End Date is "14-07-2015" then Its Not Accept. What is my Mistake? Please Guide me.
Thanks in Advance.
boolean b=false;
public boolean isDateAfter(String startDate, String endDate) {
try {
SimpleDateFormat sdf;
String myFormatString = "dd-mm-yyyy"; // for example
sdf = new SimpleDateFormat(myFormatString);
endingDate = sdf.parse(endDate);
startingDate = sdf.parse(startDate);
System.out.println("!!!!!!!!!! startingDate===="+startingDate);
System.out.println("!!!!!!!!!!! endingDate====="+endingDate);
if ((endingDate.after(startingDate))
|| (endingDate.equals(startingDate)))
b = true;
else
b = false;
} catch (Exception e) {
b = false;
}
return b;
}
Dont use SimpleDateFormat, use Time stamp like:
Long tsLong = System.currentTimeMillis() / 1000;
you can use the built in methods date.after,date.before and date.equals to do this easily
You may rely on the built-in methods like date.after() or date.before() for comparison.
For more control, you may also convert your date into long value by using date.getTime() and perform further calculations.
I have in my preferences some strings that represent a start time and and ending time.
I wrote this function to determine if the current time is within the start and ending time. The format of the date strings is "HH:mm". The function takes the strings that are from the preferences.
I'm sure I'm missing some code for the comparing because my parsing returns a something like this:
Thu Sep 29 12:24:33 EDT 2011
But all I need is to get this:
12:24
Here is the function. Can you help me correct the coding?
Thanks.
Truly,
Emad
public static boolean currentTimeIsWithinStartAndEnd(String startTime,
String endTime) {
String pattern = "HH:mm";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
boolean booleanValueToReturn = false;
try {
Date startTimeToCompare = sdf.parse(startTime);
Date endTimeToCompare = sdf.parse(endTime);
/*
* These are for the current time in date format.
*/
Date currentTime = new Date(System.currentTimeMillis());
Log.w("Emad", "Current Time: " + currentTime + " Start Time is: "
+ startTimeToCompare + " End Time is : "
+ endTimeToCompare);
/*
* Check if current time is equal or greater than the start time.
*/
if (currentTime.compareTo(startTimeToCompare) == 0
|| currentTime.compareTo(startTimeToCompare) == 1) {
booleanValueToReturn = true;
/*
* Now check if the current time is equal or less than the end
* time.
*/
if (currentTime.compareTo(endTimeToCompare) == 0
|| currentTime.compareTo(endTimeToCompare) == -1) {
booleanValueToReturn = true;
} else {
booleanValueToReturn = false;
}
} else {
booleanValueToReturn = false;
}
} catch (java.text.ParseException e) {
}
return booleanValueToReturn;
You are using SimpleDateFormat Incorrectly.
String pattern = "HH:mm" should be format in which your input Date String is. otherwise how is SimpleDateFormat going to know which portion represents what.
Create two SimpleDateFormat, f1 (with Input String Format) and f2 ( with output String Format) ;
Use f1.parse() to get Date object for Input String.
Then use f2.format() on this Date Object to get Output String representation.
Refer to SimpleDateFormat for details on how to specify date Format.
public static boolean currentTimeIsWithinStartAndEnd(String startTime,
String endTime) {
// assuming input date string is of format MM/dd/yyyy. Change it according to your needs.
String inputPattern = "MM/dd/yyyy";
String outputPattern = "HH:mm";
SimpleDateFormat inputFormatter = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormatter = new SimpleDateFormat(outputPattern);
Date startTimeToCompare = inputFormatter.parse(startTime);
String dateInRequiredFormat = outputFormat.format(startTimeToCompare);
I have a problem that I have a timestamp value which is coming from JSON parsing, I am converting that timastamp in Date Time Format which is desired, all is working fine but month always return 1 in any case. means If timestamp contains aug month but it returns only 1 for any month. I don't know how to resolve this. Please suggest me the solution for the same.
Thanks in advance.
Code:
public String create_datestring(String timestring){
try{
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(timestring));
timestring = String.valueOf(cal.get(Calendar.MONTH)+1) + "/" +String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(cal.get(Calendar.YEAR));
}catch(NumberFormatException e){
timestring = "9/23/2011";--->timestring always returns 1 for month.
}
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
}
catch(Exception ex){
return dateInStr;
}
}
Try this one It may gives you satisfied output.
String DATE_FORMAT="hh:mm aa dd MMMM yyyy";
#SuppressLint("SimpleDateFormat")
private String getDate(long time) {
return new SimpleDateFormat(DATE_FORMAT).format(time * 1000);
}