Get Account Expiration date from active directory - java

I am trying to convert Account Expires attribute of AD to date. Here is how I am trying to do it:
long adDate = Long.parseLong(adDateStr);
long milliseconds = (adDate / 10000) - DIFF_NET_JAVA_FOR_DATES;
Date date = new Date(milliseconds);
DateFormat mydate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return(mydate.format(date));
The problem is it is adding 1 day to the actual account expires day.
e.g. if the account expires date is 08/01/2106 than the code above is giving 09/01/2016.
Can anyone help me with this?

Just some guesses.
Is the value of DIFF_NET_JAVA_FOR_DATES = 11644473600000L + 24 * 60 * 60 * 1000?
The time in accountExpires and the Date is UTC time (not local).
Is this the reason?

Related

Get time from long value [duplicate]

This question already has answers here:
Android:Display time after adding GMT time zone
(2 answers)
Closed 4 years ago.
I am converting milliseconds to the time of the respective country time format, for example, pakistan, US etc
For example
timeinmilliseconds=1549362600000
So its respective Time formate from which I got these milliseconds is 15:30 or 3:30 in 12 hr format
When I want to convert these milliseconds back to that time
I get 10:30 (Five hrs back)
public String getTimeFromLong(long timeInMilliseconds){
String mytime="";
long minute = (timeInMilliseconds / (1000 * 60)) % 60;
long hour = (timeInMilliseconds / (1000 * 60 * 60)) % 24;
mytime = String.format("%02d:%02d", hour, minute);
return mytime;
}
If I select time 4:00
I converted to that to milliseconds (This part is OK)
And wants the time back from milliseconds but get five hours back
For example, If I select time 9:30
convert it to milliseconds and then to time
I get 4:30
You need to use your local time zone to get the time in your region, the default is being apllied which is the Greenwich Mean Time (GMT). For Pakistan use Asia/Karachi like so:
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("hh:mm");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Karachi"));
Use this method to convert milliseconds into your local time
public String getTime(long time){
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(time);
SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
Date date = new Date(time);
String kTime = format.format(date);
return kTime;
}
Using Java 8 we can do the following.
LocalDateTime dateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());
to get date and time
Use below code to get time from long values:
public String getTimeFromLong(long timeInMilliseconds){
// Creating date format
DateFormat simple = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");
Date result = new Date(timeInMilliseconds);
return simple.format(result);
}

How to get time with respect to another timezone in android [duplicate]

This question already has answers here:
Timezone conversion
(13 answers)
Closed 8 years ago.
I want to create an alarm application for FIFA 2014 world cup matches in which i have a server which stores the date and time for the matches in Brazil/Acre and my client is an android application and an android device may have any of the possible timezone so the my problem is i want to convert the Brazil/Acre timing to the local android device timing with different timezone and after lot of googled i came to know about joda data and time lib but it is too slow in android so please suggest any code that will work for me.
In my opinion Time class is the best for your job. Also it is Android API not general Java API.
Here I mentioned some of useful methods for your job:
void switchTimezone(String timezone)
Convert this time object so the time represented remains the same, but is instead located in a different timezone.
static String getCurrentTimezone()
Returns the timezone string that is currently set for the device.
And if you want to save a time in a timezone independed manner, you can convert to milliseconds (in UTC) by toMillis() method and then retrieve it by set(long millis) method.
If something is unclear please tell me!
UPDATE
Example:
long timeMillis = /* get time milliseconds form the server */
Time time = new Time();
time.set(timeMillis);
/* changing time zone */
time.switchTimezone(/* your desired timezone in string format */);
/* getting time as string */
String timeString = time.format("%Y%m%dT%H%M%S"); // you can change format as you wish
Here is a table for formatting times
You could use this code, which substracts the hour-difference between Brazil and the local timezone. Just replace yourDate with a Date-object.
//code...
yourDate.setTime(yourDate.getTime() - getDifferenceInMillis());
//code...
public int getDifferenceInMillis() {
// Local Time
int localMinute = Calendar.getInstance().get(Calendar.MINUTE);
int localHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int localDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
// Brazil Time
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Brazil/Acre"));
c.setTimeInMillis(new Date().getTime());
int brazilMinute = c.get(Calendar.MINUTE);
int brazilHour = c.get(Calendar.HOUR_OF_DAY);
int brazilDay = c.get(Calendar.DAY_OF_MONTH);
// Difference between Brazil and local
int minuteDifference = brazilMinute - localMinute;
int hourDifference = brazilHour - localHour;
int dayDifference = brazilDay - localDay;
if (dayDifference != 0) {
hourDifference = hourDifference + 24;
}
return (hourDifference * 60 + minuteDifference) * 60 * 1000;
}
You should store your date has a long or timestamp in your server. If you don't want you can anyway send your date as a long generate from your database date. Then create your calendar instance like that :
Calendar c = Calendar.getInstance();
c.setTime(new Date(yourdatelong));
You can have to multiplie and add some constant in your long. In java the definition is "the number of milisecond since 1970 1/1 00:00:00". It is differents in C#, for exemple ( number of nanoseconde from 1/1/1900, if I remenber well).
Like that you are sure to set the same date in all your device. When it is done, you just have to put the timezone that you want in your calendar to display the local time.
http://developer.android.com/reference/java/util/Calendar.html
You can have many option to manage time and display it in this class.
If dates are stored in server time zone (Brazil/Acre), you should load date time from DB, convert it to UTC time zone and send to client. On client side change UTC to local time zone:
Server side:
DateTime dateOnServer = // load date from db
DateTime dateUTC = dateOnServer.withZone(DateTimeZone.UTC); // convert to UTC
String dateAsStringUTC = dateUTC.toString("yyyy-MM-ddTHH:mm:ss");
// send 'dateAsStringUTC' to client
Client side:
String dateAsStringUTC = // receive date from server
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ss"); // parser for date
DateTime dateOnClient= dtf.parseDateTime(dateAsStringUTC);
// 'dateOnClient' will be in client time zone 'DateTimeZone.getDefault()'
I faced same problem like you...
I got the solution using SimpleDateFormat
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = format.parse("2011-03-01 15:10:37"); // => Date is in UTC now
TimeZone tz = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
this may help you..

How to find number of days between two unix timestamps in java

I am using unix timestamp to store the purchase date in my application.
sample data: 1371463066
I want to do some manipulation based on the difference in number of days and current day timestamp.
for example: If the number of days between the purchase date and current date is 5 days, then send an email regarding feedback again.
how to get the difference in days between two timestamps using java?
I have not tested it but you may try to do something like this:
Date purchasedDate = new Date ();
//multiply the timestampt with 1000 as java expects the time in milliseconds
purchasedDate.setTime((long)purchasedtime*1000);
Date currentDate = new Date ();
currentDate .setTime((long)currentTime*1000);
//To calculate the days difference between two dates
int diffInDays = (int)( (currentDate.getTime() - purchasedDate.getTime())
/ (1000 * 60 * 60 * 24) )
Unix timestamp is the number of seconds since 1.1.1970. If you have 2 unix timestamps then the difference in full days is
int diff = (ts1 - ts2) / 3600 / 24
You could try with Calendars (which will also allow you to use TimeZones):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1371427200l * 1000l);
Calendar newCalendar = Calendar.getInstance();
newCalendar.setTimeInMillis(1371527200l * 1000l);
// prints the difference in days between newCalendar and calendar
System.out.println(newCalendar.get(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR));
Output:
1

Add 30 minutes to the current time in java

I want to get current time in my application.
Then i want to add 30 minutes to the current time.
What is the best practice to achieve this?
I am able to get start and stop time from my web service.
eg: ((start time)11:00 am to (stop time) 11:00 pm)
now, i would like to add 30 minutes to the current time till the stop time is reached.
Calendar now = Calendar.getInstance();
now.add(Calendar.MINUTE, 30);
And to output the time you could use
// 24 hours format
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
// AM/PM format
SimpleDateFormat df = new SimpleDateFormat("hh:mm aa");
System.out.println(df.format(now.getTime()));
Use the following:
//get current Time
long currentTime = System.currentTimeMillis();
//now add half an hour, 1 800 000 miliseconds = 30 minutes
long halfAnHourLater = currentTime + 1800000;
System.currentTimeMillis()+(30*60*1000);

How to increment time by 1 hour

I have two time values. one for the previous login time and one for the current login time.
I have to increase previous time login by one hour. I have used the date format hh:mm:ss.
This is my code snippet.
Date previous_time, current_time;
if(previous_time.before(current_time)){
Log.i("Time Comparision"," true");
}
so instead of the above mentioned if condition, I have to add one hour to the previous_time and do the if condition. How to achieve this?
Calendar calendar = Calendar.getInstance();
calendar.setTime(previous_time);
calendar.add(Calendar.HOUR, 1);
previous_time = calendar.getTime();
// do your comparison
previous_time.setTime(previous_time.getTime() + 60 * 60 * 1000);
or
Date session_expiry = new Date(previous_time.getTime() + 60 * 60 * 1000);
http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getTime%28%29
Please try this code.
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
Date date = Utils.getBookingDate(mBooking.ToTime);
Calendar calendarAdd = Calendar.getInstance();
calendarAdd.setTime(date);
calendarAdd.add(Calendar.HOUR, 1);
toTime = sdf.format(calendarAdd.getTime());
tv_Totime.setText(toTime);
when current time string formate within add 1 hours

Categories