Hi I am Calling below function to find time elapsed, till that date, It is working fine for all but following input : 12:46:21 PM
public Long timeDifference(String weboutput) {
try {
Calendar calendar = GregorianCalendar.getInstance();
Calendar today = new GregorianCalendar();
Date inputTime;
if (weboutput.length() <= 10) { // for data fetched for current date.
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a", Locale.US);
inputTime = formatter.parse(weboutput);
calendar.setTime(inputTime);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
today.setTime(new Date());
today.set(Calendar.HOUR, hour);
today.set(Calendar.MINUTE, minute);
today.set(Calendar.SECOND, second);
} else {
if (weboutput.length() <= 15) { // for data for earlier date in same year or month.
DateFormat formatter = new SimpleDateFormat("MMM dd hh:mm a", Locale.US);
inputTime = formatter.parse(weboutput);
calendar.setTime(inputTime);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
today.setTime(new Date());
today.set(Calendar.HOUR, hour);
today.set(Calendar.MINUTE, minute);
today.set(Calendar.MONTH, month);
today.set(Calendar.DATE, date);
} else { // for data with different year.
DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
inputTime = formatter.parse(weboutput);
calendar.setTime(inputTime);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
int year = calendar.get(Calendar.YEAR);
today.setTime(new Date());
today.set(Calendar.HOUR, hour);
today.set(Calendar.MINUTE, minute);
today.set(Calendar.SECOND, second);
today.set(Calendar.YEAR, year);
today.set(Calendar.MONTH, month);
today.set(Calendar.DATE, date);
}
}
Date retrivedDate = today.getTime();
Calendar cal = Calendar.getInstance();
Date currentDate = cal.getTime();
difference = currentDate.getTime() - retrivedDate.getTime();
System.out.println(retrivedDate);
System.out.println(currentDate);
System.out.println(difference);
} catch (ParseException e) {
e.printStackTrace();
}
return difference;
}
public boolean alarmValue(Long alarmTime) {
if (alarmTime <= 1800000) // change this value for Alarm duration, currently 30 min = 30* 60 s = 1800 * 1000 ms = 1800000 ms.
return false;
else
return true;
}
the error is as follows:
java.text.ParseException: Unparseable date: "12:46:21 PM"
at java.text.DateFormat.parse(DateFormat.java:357)
at rcm.Selenium.Test.Calculations.timeDifference(Calculations.java:29)
at rcm.Selenium.Test.RcmSeleniumTest.main(RcmSeleniumTest.java:89)
Exception in thread "main" java.lang.NullPointerException
at rcm.Selenium.Test.Calculations.alarmValue(Calculations.java:76)
at rcm.Selenium.Test.RcmSeleniumTest.main(RcmSeleniumTest.java:90)
Kindly help me with this.
"12:46:21 PM" has 11 digits and therefore doesn't pass your first test (which is for <= 10 digits) for the format "hh:mm:ss a".
Related
My selected date in the DatePicker (not the current Date or Today Date) is date1 : SelectedDate.setText(date1) = "05-04-2022".
First i want to add 5 days to date1 to get date2 and display it in EditText2 to get: InputDate.setText(date2) = "10-04-2022".
Second add 13 days to date1 to get date3 and display it in EditText3 to get : tv_editDate.setText(date3) ="18-04-2022"
SelectedDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
sdf = new SimpleDateFormat("dd/MM/yyyy");
DatePickerDialog datePicker = new DatePickerDialog(Activity_races.this, new
DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int
dayOfMonth) {
String date1 = String.valueOf(dayOfMonth) + "/" +
String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);
SelectDate.setText(date1);
// Todoo .. add 5 days to date1
inputDate.setText(Date2);
// Todoo .. add 13 days to date1
tv_editDate.setText(date3);
}
}, yy, mm, dd);
datePicker.show();
}
});
I found the solution and if there is another proposal it is with pleasure:
inputLabel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
sdf = new SimpleDateFormat("dd/MM/yyyy");
DatePickerDialog datePicker = new DatePickerDialog(Activity_races.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date1 = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);
// Todoo
//Given Date in String format
String oldDate = date1;
//Specifying date format that matches the given date
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
Calendar c1 = Calendar.getInstance();
try{
//Setting the date to the given date
c.setTime(sdf.parse(oldDate));
c1.setTime(sdf.parse(oldDate));
}catch(ParseException e){
e.printStackTrace();
}
//Number of Days to add
c.add(Calendar.DAY_OF_MONTH, 5);
c1.add(Calendar.DAY_OF_MONTH, 13);
//Date after adding the days to the given date
String Date2 = sdf.format(c.getTime());
String Date3 = sdf.format(c1.getTime());
//Displaying the new Date after addition of Days
SelectDate.setText(date1);
inputDate.setText(Date2);
tv_Date.setText(Date3);
}
}, yy, mm, dd);
datePicker.show();
}
});
SimpleDateFormat format3 = new SimpleDateFormat("yyyy/MM/dd");
String birthDate = format3.format(date);
try {
age = format3.parse(birthDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
if(age != null)
c.setTime(age);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date2 = c.get(Calendar.DATE);
LocalDate l1 = LocalDate.of(year,month,date2);
LocalDate now = LocalDate.now();
Period diff = Period.between(l1,now);
Integer age = diff.getYears();
String ageString = age.toString();
This would be my approach using LocalDate and DateTimeFormatter. It is not only shorter, but also less error-prone.
String inputDate = "1990/01/21";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate age = LocalDate.parse(inputDate, formatter);
Period diff = Period.between(age, LocalDate.now()); // outputs: 31 years
private void startDate(String selected_date) {
if (selected_date != null && selected_date != "0") {
Calendar mcurrentDate = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
int selectDate = Integer.parseInt(selected_date);
mcurrentDate.add(Calendar.MONTH, 1);
mcurrentDate.set(Calendar.DAY_OF_MONTH, selectDate);
long diff = mcurrentDate.getTimeInMillis() - calendar.getTimeInMillis();
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
if (days < 60) {
mcurrentDate.add(Calendar.MONTH, 1);
long yourmilliseconds = mcurrentDate.getTimeInMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
Log.e("day60", sdf.format(resultdate));
} else {
long yourmilliseconds = mcurrentDate.getTimeInMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
Log.e("elseday60", String.valueOf(mcurrentDate.getTimeInMillis()));
}
}
}
I NEED IF USER PUT 9TH in date THEN IT SHOULD REVERT BACK date BETWEEN 60 TO 90Days date not in between 0 to 30days date
Getting a date for a particular day-of-month that is at least e.g. 60 days into the future can be done like this:
private static Date startDate(int day) {
if (day < 1 || day > 31)
throw new IllegalArgumentException("Invalid day: " + day);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 60);
long minMillis = cal.getTimeInMillis();
if (day > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) // if day would overflow:
cal.add(Calendar.MONTH, 1); // skip to next month which will be 31 days long
cal.set(Calendar.DAY_OF_MONTH, day);
if (cal.getTimeInMillis() < minMillis) {
cal.set(Calendar.DAY_OF_MONTH, 1); // prevent day-overflow when adding 1 month
cal.add(Calendar.MONTH, 1);
if (day > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) // if day would overflow:
cal.add(Calendar.MONTH, 1); // skip to next month which will be 31 days long
cal.set(Calendar.DAY_OF_MONTH, day);
}
return cal.getTime();
}
Test
System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(9)));
System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(30)));
System.out.println(new SimpleDateFormat("MMM dd,yyyy HH:mm").format(startDate(31)));
Sample Output (today is Sep 12, 2020)
Dec 09,2020 09:53
Nov 30,2020 09:53
Dec 31,2020 09:53
Notice how asking for the 31st needs to skip all the way to Dec 31, because Oct 31 is only 49 days and Nov doesn't have a 31st, so the next 31st is Dec 31, a whopping 110 days into the future.
I'm using the code below to check if an hour is between two other specific hours:
String openHour = "08:00 AM";
String currentHour = "10:00 PM";
String closeHour = "11:00 PM"; //Change to 02:00 AM doesn't work!!!
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date openHourDate = format.parse(openHour);
Date currentHourDate = format.parse(currentHour);
Date closeHourDate = format.parse(closeHour);
Calendar openCalendar = Calendar.getInstance();
openCalendar.setTime(openHourDate);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(currentHourDate);
Calendar closeCalendar = Calendar.getInstance();
closeCalendar.setTime(closeHourDate);
Date open = openCalendar.getTime();
Date current = currentCalendar.getTime();
Date close = closeCalendar.getTime();
if (current.after(open) && current.before(close)) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect!");
}
If the currentHour is "10:00 PM" as you see in my code, everything works fine but if I change the change it to "02:00 AM", the code doesn't work as expected even if the currentHour is between 08:00 AM and 02:00 AM. How to solve this?
Here is a solution using LocalTime that also correctly handles current and closing time being after midnight.
String openHour = "08:00 AM";
String currentHour = "01:00 PM";
String closeHour = "02:00 AM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "hh:mm a" , Locale.US );
LocalTime openTime = LocalTime.parse(openHour, formatter);
LocalTime currentTime = LocalTime.parse(currentHour, formatter);
LocalTime closeTime = LocalTime.parse(closeHour, formatter);
boolean isOpen = false;
if (closeTime.isAfter(openTime)) {
if (openTime.isBefore(currentTime) && closeTime.isAfter(currentTime)) {
isOpen = true;
}
} else if (currentTime.isAfter(openTime) || currentTime.isBefore(closeTime)) {
isOpen = true;
}
if (isOpen) {
System.out.println("We are open");
} else {
System.out.println("We are closed");
}
Just need to roll the day, as Michael Platt suggested:
import java.util.*;
import java.text.*;
public class Foo {
public static void main(String[] args) throws Exception {
String openHour = "08:00 AM";
String currentHour = "10:00 PM";
String closeHour = "11:00 PM"; //Change to 02:00 AM doesn't work!!!
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date openHourDate = format.parse(openHour);
Date currentHourDate = format.parse(currentHour);
Date closeHourDate = format.parse(closeHour);
Calendar openCalendar = Calendar.getInstance();
openCalendar.setTime(openHourDate);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(currentHourDate);
Calendar closeCalendar = Calendar.getInstance();
closeCalendar.setTime(closeHourDate);
if (closeCalendar.before(openCalendar)) {
closeCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
Date open = openCalendar.getTime();
Date current = currentCalendar.getTime();
Date close = closeCalendar.getTime();
if (current.after(open) && current.before(close)) {
System.out.println("Correct!");
} else {
System.out.println("Incorrect!");
}
}
}
How to find previous date if current date is given as a String? Below is given my code. Is there any shorter solution?
private static String previousDay(String date) {
String[] ymd = date.split("-");
int year = Integer.parseInt(ymd[0]);
int month = Integer.parseInt(ymd[1]);
int day = Integer.parseInt(ymd[2]);
String newDate = "";
if (day > 1 & month > 1)
newDate = year+"-"+month+"-"+(day-1);
else if (day == 1 & month > 1) {
Calendar calendar = new GregorianCalendar(year,month-1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
newDate = year+"-"+(month-1)+"-"+daysInMonth;
} else if (day == 1 & month == 1) {
Calendar calendar = new GregorianCalendar(year,12, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
newDate = year+"-"+12+"-"+daysInMonth;
}
return newDate;
}
You need to convert your String to Date, in order to do date calculations. You can use Calender to find previous day. From your code, I assume, your date format is yyyy-MM-dd.
String input = "2009-09-30";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = dateFormat.parse(input);
Calendar cal1 = Calendar.getInstance();
cal1.setTime(myDate);
cal1.add(Calendar.DAY_OF_YEAR, -1);
Date previousDate = cal1.getTime();
Date currentDate= new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date previousDate = calendar.getTime();
// dccTimeStamp is "20120122121212"
String dccTimeStamp is "20120122121212"
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date dcc = sdf.parse(dccTimeStamp);
log.debug("Dcc Date is " + dcc.toString());
From this point on you can use Date (or Calendar) utilities and perform the Date operation you need.