I have 09:30 as a time retrieved from a database, I need to add 15 minutes to get it as 09:45.
Is there a function for adding time?
String RevisedTime=cursor.getString(cursor.getColumnIndex("RevisedEstimatedDeliveryTime"));
// get hour and minute from time string
StringTokenizer st1 = new StringTokenizer(RevisedTime, ":");
int j = 0;
int[] val = new int[st1.countTokens()];
// iterate through tokens
while (st1.hasMoreTokens()) {
val[j] = Integer.parseInt(st1.nextToken());
j++;
}`enter code here`
// call time add method with current hour, minute and minutesToAdd,
// return added time as a string
String dateRevisedEstimatedDeliveryTime = addTime(val[0], val[1], 15);
public String addTime(int hour, int minute, int minutesToAdd) {
Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
calendar.add(Calendar.MINUTE, minutesToAdd);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(calendar.getTime());
return date;
}
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html has what you need, using its add() method.
Try like this
final Calendar cld = Calendar.getInstance();
cld.setTimeInMillis(time);
cld.add(Calendar.MINUTE,15)
use Calendar.add(Calendar.MINUTE,15) to get that
Related
I have a start date = "13:45"
and an end date = "14:35"
Now i would like to check if the system time is between these two times, for example "14:00"
I have tried the methods before and after from the Date class but it did not worked properly
Any Suggestions ?
SimpleDateFormat format = new SimpleDateFormat("HHmm",
Locale.getDefault());
String now = format.format(new Date());
String start = "13:45".replace(":", "");
String end = "14:35".replace(":", "");
boolean isBetween = Integer.valueOf(now) > Integer.valueOf(start)
&& Integer.valueOf(now) < Integer.valueOf(end);
System.out.println(isBetween);
Maybe you could do something like this:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, 13);
start.set(Calendar.MINUTE, 45);
Calendar current = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, 14);
end.set(Calendar.MINUTE, 35);
if(current.after(start) && current.before(end)){
//we are between
}
It can be the similar question that I have found in StackOverFlow. But I have a little bit different situation then all of them. In my database I have start Date and Start Time. I want the system compare it's date and time separately because starting date and time are in separate columns in database. First the system compares the date and then time. The another activity should open only if the date is same and the time is before 15 minutes the starting time in database.
I have done this so far;
private void startTraining() {
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyy");
String currentDate = formatter.format(calendar1.getTime());
System.out.println(currentDate);
Calendar calendar2 = Calendar.getInstance();
SimpleDateFormat formatter2 = new SimpleDateFormat("hh:mm");
String currentTime = formatter2.format(calendar2.getTime());
System.out.println(currentTime);
String trainingStartDate = SharedMemory.getInstance()
.getCurrentTraining().getDate();
String trainingStartTime = SharedMemory.getInstance()
.getCurrentTraining().getStartTime();
int difference = trainingStartTime.compareTo(currentTime);
System.out.println(difference);
//System.out.println(trainingStartTime);
if (currentDate.toString().equals(trainingStartDate)&& difference < 15) {
Log.i("Debug", "CHECKPOINT");
Intent intent = new Intent(getApplicationContext(),
TraineeListActivity.class);
MainActivity.this.startActivity(intent);
finish();
} else{
}
}
I am not getting the result what I am expecting. Thanks in advance.
You use String.compareTo() to compare the "time" String, which won't work correctly. Try changing the code to this
private void startTraining() throws ParseException {
// current date & time
Calendar now = Calendar.getInstance();
// parse date & time from database
String trainingStartDate = SharedMemory.getInstance()
.getCurrentTraining().getDate();
String trainingStartTime = SharedMemory.getInstance()
.getCurrentTraining().getStartTime();
String strDateTime = trainingStartDate + " " + trainingStartTime;
Calendar training = Calendar.getInstance();
training.setTime(new SimpleDateFormat("MM-dd-yyyy kk:mm")
.parse(strDateTime));
// find difference in milliseconds
long difference = training.getTimeInMillis() - now.getTimeInMillis();
if (difference < 15 * 60 * 1000) { //less than 15 minutes
Log.i("Debug", "CHECKPOINT");
Intent intent = new Intent(getApplicationContext(),
TraineeListActivity.class);
MainActivity.this.startActivity(intent);
finish();
} else {
}
}
I am using android.text.format.Time and i need to have a function which gets a string and converts that string into a Time object. I initially stored the time object as string for some other reason. I just need to pasre teh string into Time object now.
Code:
Time time = new Time();
String time = time.toString();
Time t = new Time();
t.parse(time);
this.time = t;
Now
t.parse(time)
gives a boolean value.
boolean value = time.parse(s)
Parameters:
time - the string to parse
Returns:
true - if the resulting time value is in UTC time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2012-12-14 14:05:59");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date.getTime());
Time time = new Time();
time.set(int second, int minute, int hourOfDay, int monthDay, int month, int year);
// int second => calendar.get(Calendar.SECOND);
// etc minute, hourOfDay, monthOfDay, month, year...
I had written a function for Adding time as given below
private void Delay15Minute() {
String pkManifest = manifest.pkManifestNo;
manifest_helper = new manifest_helper(this);
cursor = manifest_helper.GetDeliveries(pkManifest);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.getString(cursor.getColumnIndex("PKDelivery"));
// String
// RevisedTime=cursor.getString(cursor.getColumnIndex("RevisedEstimatedDeliveryTime"));
String RevisedTime = "12:55";
// get hour and minute from time string
StringTokenizer st1 = new StringTokenizer(RevisedTime, ":");
int j = 0;
int[] val = new int[st1.countTokens()];
// iterate through tokens
while (st1.hasMoreTokens()) {
val[j] = Integer.parseInt(st1.nextToken());
j++;
}
// call time add method with current hour, minute and minutesToAdd,
// return added time as a string
String date = addTime(val[0], val[1], 15);
// Tioast the new time
Toast.makeText(this, "date is =" + date, Toast.LENGTH_SHORT).show();
}
}
public String addTime(int hour, int minute, int minutesToAdd) {
Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
calendar.add(Calendar.MINUTE, minutesToAdd);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
String date = sdf.format(calendar.getTime());
return date;
}
I am getting the oupt of this as 01:10 as 12 hours fromat...
I need to get it in 13:10 format ie 24 hour format.....Please help me
You used hh in your SimpleDateFormat pattern. Thats the 12 hour format. Use kk instead, that gives you the hours of the day in a 24 hour format. See SimpleDateFormat.
Simply create the instance of Calendar and get 24 hr time by,
Calendar c = Calendar.getInstance();
int Hr24=c.get(Calendar.HOUR_OF_DAY);
int Min=c.get(Calendar.MINUTE);
Use this code
long date = System.currentTimeMillis();
SimpleDateFormat date1 = new SimpleDateFormat("dd-MM-yyyy"); // for current date
SimpleDateFormat time1 = new SimpleDateFormat("kk:mm:ss"); // for 24 hour time
SimpleDateFormat time2 = new SimpleDateFormat("hh:mm:ss"); // for 12 hour time
String dateString = date1.format(date); //This will return current date in 31-12-2018 format
String timeString1 = time1.format(date); //This will return current time in 24 Hour format
String timeString2 = time2.format(date); //This will return current time in 12 Hour format
Log.e("TAG_1", "24 hour Time - " + timeString1);
Log.e("TAG_1", "24 hour Time - " + timeString1);
Log.e("TAG_1", "dd-MM-yyyy Date format - " + dateString);
than open your logcat to check result.
i have a android timepicker, and i need to get his time in java code, and transform it into a string with this appereance: "08:00:00" (hours, mins, secs)
can someone help me to do it in a easy way?
code example will be appreciated
TimePicker t = new TimePicker(this);
String formattedTime = "";
int hour = t.getCurrentHour();
String sHour = "00";
if(hour < 10){
sHour = "0"+hour;
} else {
sHour = String.valueOf(hour);
}
int minute = t.getCurrentMinute();
String sMinute = "00";
if(minute < 10){
sMinute = "0"+minute;
} else {
sMinute = String.valueOf(minute);
}
formattedTime = sHour+":"+sMinute+":"+"00"; // Sorry you can't get seconds from a TimePicker
TimePicker has 2 methods available to get the set time. getCurrentHour and getCurrentMinute.
So outputting this as a string shouldn't be too hard.
String s;
Format formatter;
Calendar calendar = Calendar.getInstance();
// tp = TimePicker
calendar.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
calendar.set(Calendar.MINUTE, tp.setCurrentMinutes());
calendar.clear(Calendar.SECOND); //reset seconds to zero
formatter = new SimpleDateFormat("HH:mm:ss");
s = formatter.format(calendar.getTime()); // 08:00:00
By the way, lowercase hh will get you a 12 hour clock.
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
String formatted = format.format(date); // date is a long in milliseconds
Just use SimpleDateFormat to format date and time .
Calendar cal=new Calendar();
SimpleDateFormat frmDate=SimpleDateFormat("dd-MM-yyyy");
String s=frmDate.format(cal.getTime());
SimpleDateFormat frmTime=SimpleDateFormat("HH:MM:SS");
String t=frmTime.formate(cal.getTime());
http://developer.android.com/reference/java/text/SimpleDateFormat.html
http://developer.android.com/reference/java/text/DateFormat.html
Also check out DateUtils, very useful.
Please find the below code:
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
String formatted = format.format(date); // date is a long in milliseconds