Get the current time in a given time range - android

So I have a time range example:
2016-09-26T12:00:00.000+08:00 / 2016-09-27T11:59:00.000+08:00
Assuming the current time is 2016-09-29T06:00:00.000+08:00
So what I want is to get which of dates on the said time range (2016-09-26 or 2016-09-27) the 06:00AM falls...
I tried using joda's Interval class but I can't figure out how to make it work. any suggestion is appreciated.
sample expected outputs:
06:00AM => 2016-09-27
06:00PM => 2016-09-26
03:00AM => 2016-09-27
03:00PM => 2016-09-26

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
String strEndDate = "2016-09-27T11:59:00.000+0800";
SimpleDateFormat fmt = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:MM:ss.SSSZ");
Date endDate = null;
try {
endDate = fmt.parse(strEndDate);
} catch (Exception e) {
// TODO: handle exception
}
Date currDate = new Date(); // you can set your date and time here
while(currDate.after(endDate)){
currDate.setTime(currDate.getTime()-(24*60*60*1000));
}
String result = fmt.format(currDate);

I think I found a way that works for me
DateTime beginTime = getStartTime();
DateTime withTimeNow = beginTime.withTime(LocalTime.now())
LocalDate result = withTimeNow.isBefore(beginTime)? beginTime.toLocalDate().plusDays(1) : beginTime.toLocalDate();
I'm not sure if it's efficient but it does what I need.

Related

how to find difference between two date Time string?

I am calculating time difference between two dates and time but its restuning invalid difference.
here's my sample date and code to calculate the difference.
loginTime=2016-01-24 12:04:30.16
expiryTime = 2016-01-24 13:04:30.16
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date now = new Date();
try {
Date startDate = simpleDateFormat.parse(loginTime);
Date expireDate = simpleDateFormat.parse(expiryTime);
// String temCurrentDate = new SimpleDateFormat("yyyy-MM-dd").format(now);
// Date currentDate = new SimpleDateFormat("yyyy-MM-dd").parse(temCurrentDate);
// String day_of_week = new SimpleDateFormat("EEEE").format(now);
long difference =startDate.getTime()- expireDate.getTime();
int hour = (int) difference / (60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
Difference between this time is 1 hour but i am getting 11 or 13. Please tell where i am going wrong in this.
Spelling error in your variable:
simpleDateFormat.parse(expiryTime);
Does not match: ExpiryTime = 2016-01-24 13:04:30.16
That's the whole problem.

How to get local time Android

I have a String of a date and time in my app eg:- 2014-10-30T11:30:00 I want to convert this String into my local time which should look something like this
2014-10-30T11:30:00+05:30. I cannot Manipulate the String as the server side conversion is done to do the addition and subtraction of the time. How do I add the +05:30 offset to my time?
How can I do it using the local time so that the server knows my locale?
Try this way,hope this will help you to solve your problem.
How to use given function :
convertDateStringFormat("2014-10-30T11:30:00","yyyy-MM-dd'T'hh:mm:ss","yyyy-MM-dd'T'hh:mm:ss")
Here you have pass three parameter as :
1.strDate : which is represent datetime string.
2.fromFormat : which is represent datetime format of your given datetime string.
3.toFormat : which is represent datetime format which you wan to convert your given datetime string.
public String convertDateStringFormat(String strDate, String fromFormat, String toFormat){
try{
SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormat2 = new SimpleDateFormat(toFormat.trim());
return dateFormat2.format(sdf.parse(strDate));
}catch (Exception e) {
e.printStackTrace();
return "";
}
}
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
sdf.setTimeZone(tz);
long timestamp = cursor.getLong(columnIndex);
Log.d("Server time: ", timestamp);
String localTime = sdf.format(new Date(timestamp * 1000));
Log.d("Time: ", localTime);

How to compare two Date and Time and get Day,Month,hour,min in Android?

I Have this type of Date in String Format 01-18-2013 06:43:35 Now, i want to compare this Date with Current Date and Time and get Day, Month, Hour, Min, .. I Searched this link but didn't get any Solution...Please share some solution..Thank you..
This might help,
http://developer.android.com/reference/java/text/DateFormat.html
You can parse the Date from string using
DateFormat df = DateFormat.getDateInstance();
myDate = df.parse(myString);
If I get your question, you would like to Compare a Date object with the current date..
Let's say that 'date' is the Date object you want to compare with the current date:
Why don't you just do something like date.after(new Date()) or date.before(new Date()) as suggested form the android doc?
You can get UTC with
new SimpleDateFormat("MM-dd-yyyy HH:mm:ss").parse("01-18-2013 06:43:35").getTime();
Then compare the result with
System.currentTimeMillis();
this may helps you to calculate to diffrent date time in millisecond
try{
Calendar calender = Calendar.getInstance();
Calendar calDb = Calendar.getInstance();
Calendar matchd = Calendar.getInstance();
mYear = calender.get(Calendar.YEAR);
mMonth = calender.get(Calendar.MONTH);
mDay = calender.get(Calendar.DAY_OF_MONTH);
mendYear = calDb.get(Calendar.YEAR);
mendMonth = calDb.get(Calendar.MONTH);
mendDay = calDb.get(Calendar.DAY_OF_MONTH);
// Here you can change day values
calDb.set(Calendar.DAY_OF_MONTH, mDay-1);
strbeforedate = mDateFormat.format(calDb.getTime());
curentdate = mDateFormat.format(calender.getTime());
calDb.setTime(mDateFormat.parse(strbeforedate));
calender.setTime(mDateFormat.parse(curentdate));
String mydate = "2013.03.14 03:11";
String mdatetime = "";
deletepath = new ArrayList<String>();
for(int i=0;i<result.length;i++){
try{
// here your matching goes and pass date here
matchd.setTime(mDateFormat.parse(mdatetime));
long diff = calDb.getTimeInMillis() - calender.getTimeInMillis();
long matchdiff = matchd.getTimeInMillis() - calender.getTimeInMillis();
if(diff < matchdiff){
// do your work here
}else{
// do your else work here
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}

How can i convert dropbox log timestamp?

I am debugging a android framework.
I pull out dropbox logs from device and it's created in /data/system/dropbox.
Log file name is printed like this format.
event_data#1362451303699
1362451303699 is timestamp and i want to change it like 05/03/2013 16:00 for legibility.
How can i convert this timestamp?
Is there any code needs to be changed?
Any help will be much appreciated.
use: Date date = new Date(timestamp);
Edit full code:
String wantedDate = "";
String log = "event_data#1362451303699";
int index = log.indexOf("#");
if(index != -1) {
index = index + 1; // skip # symbol
if(index < log.length()) { // avoid out of bounds
String logtime = log.substring(+1);
try {
long timestamp = Long.parseLong(logtime);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = new Date(timestamp);
wantedDate = df.format(date);
} catch (NumberFormatException nfe) {
// not a number
}
}
}
if( ! "".equals(wantedDate) ) {
// everything OK
} else {
// error cannot retrieve date!
}
Related doc:
indexOf : http://developer.android.com/reference/java/lang/String.html#indexOf%28java.lang.String%29
SimpleDateFormat : http://developer.android.com/reference/java/text/SimpleDateFormat.html
you can use a SimepleDateFormat to parse it. For example:
long ts = 1362451303699;
Date date = new Date(ts);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(sdf.format(date));
With the SimpleDateFormat you can bring your Date in a more readable format.
It is a UNIX epoch timestamp, all you need to do is to convert the String representation of the number to long, then you can use it to create a Date object, which you can format with DateFormat. Something like this:
// Get this from the log
String timestamp = "1362451303699";
long epoch = Long.parseLong(timestamp);
Date date = new Date(epoch);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String formattedDate = format.format(date);

Convert a combined Date and Time String to long value?

I am trying to get long value of a string (date & time string), but it is not working. What I am trying to do is:
Choose date form datepicker and store it in a String
Choose time from timepicker and store it in a String
Then I concatenate these two strings and get long value from that string.
I have tried a few Date formatters but I am unable to get this done. The format of my string is dd-MM-yyy h:mm a. Please help me out of this. Provide any utility available for this.
Try this:-
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyy h:mm a");
Date myDate = new Date(); // Default Value.
try {
myDate = sdf.parse(dateString);
} catch (ParseException e) {
// Do Something on Error.
}
Long dateTimeinLong = myDate.getTime();
where dateString is your concatenated String of date and time.
Forget about strings, and go directly with the values:
DatePicker dp = (DatePicker) findViewById...
TimePicker tp = (TimePicker) findViewById...
Date timeStamp = new Date( dp.getYear(), dp.getMonth(), dp.getDay(), tp.getHour(), tp.getMinute(), 0 );
long longTime = timeStamp.getTime();

Categories

Resources