How to check time is between two time only night in android - java

in my android app I have the condition if selected time is greater than 11:00 PM and 07:00 AM then there will be extra charges. it applicable only at night. currently, my code is work correctly but when I am selecting a time in day condition is getting true.
here is my code
Calendar calendar = Calendar.getInstance();
String startDatetime = "11:00 PM";
String endDatetime = "07:00 AM";
SimpleDateFormat formatfornightcharges = new SimpleDateFormat("HH:mm aa");
int Hr24 = calendar.get(Calendar.HOUR_OF_DAY);
try {
Date startDate = formatfornightcharges.parse(startDatetime);
Date selectedTimeforBooking = formatfornightcharges.parse(time_for_night);
Date endDate = formatfornightcharges.parse(endDatetime);
if (selectedTimeforBooking.after(startDate) || selectedTimeforBooking.before(endDate)) {
Toast.makeText(context, "200", Toast.LENGTH_SHORT).show();
night_extra_charges = 200;
tv_text.setText("" + night_extra_charges);
} else {
night_extra_charges = 0;
tv_text.setText("" + night_extra_charges);
Toast.makeText(context, "0", Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
e.printStackTrace();
}
}

You only need to change the formatter to this:
SimpleDateFormat formatfornightcharges = new SimpleDateFormat("hh:mm aa", Locale.US);
instead of "hh:MM aa".

Try this ,it is working for all time, only change time_for_night in this format :-
String startDatetime = "11:00 PM";
String endDatetime = "07:00 AM";
String time_for_night = "11:05 PM";
Integer night_extra_charges=0;
SimpleDateFormat formatfornightcharges = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH);
try {
Date startDate = formatfornightcharges.parse(startDatetime);
Date selectedTimeforBooking = formatfornightcharges.parse(time_for_night);
Date endDate = formatfornightcharges.parse(endDatetime);
if ((selectedTimeforBooking.after(startDate)) || (selectedTimeforBooking.before(endDate))) {
Toast.makeText(ScanDocumentsActivity.this, "200", Toast.LENGTH_SHORT).show();
night_extra_charges = 200;
Log.wtf("night_extra_charges",""+night_extra_charges);
} else {
night_extra_charges = 0;
Log.wtf("night_extra_charges",""+night_extra_charges);
Toast.makeText(ScanDocumentsActivity.this, "0", Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}

Related

how to convert multiple format of dates into only one format using java

from the database postgressql i'm getting date sometime date in different different forlamts like "2015-11-26 09:30:10","2015-11-26 09:30:10.080","2015-11-26 09:30:10.000" now i want to convert all format as only this format 22/01/2018 how to do in java.
try {
Date theDate1 = new Date("JAN 13,2014 09:15");
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
} catch (Exception e {
System.out.println(e.toString());
}
i got output as Hello, World !13 / 01 / 2014 but when i tried
try {
Date theDate1 = new Date("2017-11-27 00:00:00");
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
} catch (Exception e) {
System.out.println(e.toString());
}
then i got java.lang.IllegalArgumentExceptionhow to solve this problem
Try this
String text = "JAN 13,2014 09:15";
String patternst = "\\d\\d\\d\\d-\\d\\d-\\d\\d*";
Pattern pattern = Pattern.compile(patternst);
Matcher matcher = pattern.matcher(text);
String data = "";
while (matcher.find()) {
data = matcher.group(0);
}
try {
int year = Integer.parseInt(data.split("-")[0]);
int month = Integer.parseInt(data.split("-")[1]);
int day = Integer.parseInt(data.split("-")[2]);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
Date theDate1 = cal.getTime();
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
} catch (Exception e) {
Date theDate1 = new Date(text);
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
}

How to compare time exist between two times in android

String time1="09-00 AM"; String time2="07-00 PM";
String date= 15-09-2017 04:04:33
String time1 = "09-00 AM";
String time2 = "07-00 PM";
public static void main(String[] args) {
try {
DateFormat df = new SimpleDateFormat("HH:mm:SS");
Date time1 = df.parse("09-00 AM");
Date time2 = df.parse("07-00 PM");
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sf.format(date);
TimeZone tz1 = TimeZone.getTimeZone("PST");
Date c = shiftTimeZone(date, tz1);
System.out.println("Format : " + new SimpleDateFormat("HH:mm:SS").format(c));
System.out.println(time1 + "" + time2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
How can I compare that date time is exist between time1 and
time2 please suggest how I can check this, I am not able to split time or merge with date.
For that I am trying to Convert time1 and time2 in Date object in HH:mm:SS
Use .after method to compare two dates
check this link for more details
public static void main(String[] args) {
try {
SimpleDateFormat df = new SimpleDateFormat("HH:mm a");
Date time1 = df.parse("09-00 AM");
Date time2 = df.parse("07-00 PM");
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//converts date to required format
String current = sf.format(date);
Date currentTime = df.parse(current);
// TODO COMPARE currentTime with time1 or time2 as per your need
System.out.println(time1 + "" + time2 +" " + currentTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

adding date to calendar is not updating month [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
I'm working on an android application and new to it.
I have to get date from user and then add 28 days and store it in database.
This is what I have done so far
private void saveDate() throws ParseException {
DatabaseHelper db = new DatabaseHelper(ActivityPeriodToday.this.getActivity());
String pDate = periodDate.getText().toString().trim();
String pTime = periodTime.getText().toString().trim();
String next_expected = getNextExpected(pDate);
boolean isInserted = db.insertPeriodTodayIntoPeriods(pDate, pTime, early_late, pDifference, pType, next_expected);
if (isInserted == true) {
Toast.makeText(ActivityPeriodToday.this.getActivity(), "Saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ActivityPeriodToday.this.getActivity(), "Could not be saved", Toast.LENGTH_SHORT).show();
}
}
private String getNextExpected(String pDate) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(pDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, 28);
return sdf.format(c.getTime());
}
But is code is not incrementing month.
Ex. If user selects 01/11/2016, then date is incremented and is saved
29/11/2016. But if user selects 16/11/2016 then saves date is
28/11/2016 but this should be 14/12/2016
Step 1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dateInString));
Step-2 use add() to add number of days to calendar
c.add(Calendar.DATE, 40);
Try using this:
calendar.add(Calendar.DAY_OF_YEAR, 28);
Its Working for me.
Calendar c = Calendar.getInstance();
int Year = c.get(Calendar.YEAR);
int Month = c.get(Calendar.MONTH);
int Day = c.get(Calendar.DAY_OF_MONTH);
// current date
String CurrentDate = Year + "/" + Month + "/" + Day;
String dateInString = CurrentDate; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dateInString));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 28);//insert the number of days that you want
sdf = new SimpleDateFormat("dd/MM/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
Toast.makeText(MainActivity.this, ""+dateInString, Toast.LENGTH_SHORT).show();
Your question may already have an answer here: How can I increment a date by one day in Java?
Or you can simply use
c.add(Calendar.DATE, 28);
instead of
c.add(Calendar.DAY_OF_MONTH, 28);

My Logic is working fine, but seems too vague.Is there any other easy way to convert "Aug/2016" to "08/2016" in java?

Is there any other easy way to convert "Aug/2016" to "08/2016" in java?
My Logic is working fine, but seems too vague.
String myDate = "Aug/2016";
String str = myDate.split("/")[0];
Date date;
try {
date = new SimpleDateFormat("MMMM").parse(str);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println("##### ----- month in number : " +cal.get(Calendar.MONTH+1));
int year = Calendar.getInstance().get(Calendar.YEAR);
int mnth = cal.get(Calendar.MONTH) + 1;
str = String.valueOf(mnth+"/"+year);
System.out.println("##### ----- new selected date : " +str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
suggestions please!...
Simply use SimpleDateFormat:
String myDate = "Aug/2016";
SimpleDateFormat parser = new SimpleDateFormat("MMM/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy");
System.out.println(formatter.format(parser.parse(myDate)));

Java: Date Time Conversion

I have a date format like "SA25MAY"; I need to convert it into date time variable and then I want to add one day in that. And then I need to return the answer in same format. Please do some needful
try {
String str_date = "SA25MAY";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("ddd-dd-MMM");
date = (Date) formatter.parse(str_date);
System.out.println("Today is " + date);
} catch (Exception e) {
e.printStackTrace();
}
ERROR:
java.text.ParseException: Unparseable date: "SA25MAY"
at java.text.DateFormat.parse(DateFormat.java:337)
at javadatatable.JavaDataTable.main(JavaDataTable.java:29)
Here I don't know how to resolve this problem.
ddd can not match SUN. Use EEE instead if you want to match the day name in the week.
You can only add one day if you know the year because of leap years (29th of February).
In case the year is the current year, the following solution should do the the job:
For "SA25MAY":
try {
String str_date = "SA25MAY";
// remove SA
str_date = str_date.replaceFirst("..", "");
// add current year
Calendar c = Calendar.getInstance();
str_date = c.get(Calendar.YEAR) + str_date;
// parse date
Date date;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyddMMM");
date = formatter.parse(str_date);
System.out.println("Today is " + date);
// add day
c.setTime(date);
c.add(Calendar.DATE, 1);
// rebuild the old pattern with the new date
SimpleDateFormat formatter2 = new SimpleDateFormat("EEEddMMM");
String tomorrow = formatter2.format(c.getTime());
tomorrow = tomorrow.toUpperCase();
tomorrow = tomorrow.substring(0, 2) + tomorrow.substring(3);
System.out.println("Tomorrow is " + tomorrow);
} catch (Exception e) {
e.printStackTrace();
}
Or for "SA-25-MAY":
try {
String str_date = "SA-25-MAY";
// remove SA
str_date = str_date.replaceFirst("..-", "");
// add current year
Calendar c = Calendar.getInstance();
str_date = c.get(Calendar.YEAR) + "-" + str_date;
// parse date
Date date;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MMM");
date = formatter.parse(str_date);
System.out.println("Today is " + date);
// add day
c.setTime(date);
c.add(Calendar.DATE, 1);
// rebuild the old pattern with the new date
SimpleDateFormat formatter2 = new SimpleDateFormat("EEE-dd-MMM");
String tomorrow = formatter2.format(c.getTime());
tomorrow = tomorrow.toUpperCase();
tomorrow = tomorrow.substring(0, 2) + tomorrow.substring(3);
System.out.println("Tomorrow is " + tomorrow);
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources