I'm trying to convert this time stamp value to date but its giving me wrong time. date is correct
TimeStamp : 1423821615
True Value : Fri, 13 Feb 2015 10:00:15 GMT
Android Code shows : Fri, 13 Feb 2015 15:30:15 IST
Here is the code I'm using to convert time stamp to date.
Date dt = new Date((long)timestampInSeconds * 1000);
I tried this code too but same result
public static Date getDateFromTimeStamp(long timestampInMilliseconds) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestampInMilliseconds);
return cal.getTime();
}
Date dt = getDateFromTimeStamp((long)timestampInSeconds * 1000);
I don't know what I'm doing wrong. Please help
Now I explain the whole scenario. My client is from UK and I'm from India (+5:30 ahead). He created appointment for 10 AM in UK obviously. But now I have his database in my local PC. My .NET software it shows same time as it shows in below image of SQL server. But in mobile, it doesn't. PC and mobile both are in same time zone.
I use this code to convert date to time stamp and send this time stamp to mobile app through web service
SELECT DATEDIFF(SECOND,{d '1970-01-01'}, Appointments.DateTime) AS AppointmentTimeStamp FROM Appointments
Here is image of what my .NET software displays
does it matter that record was created when database was in UK time zone. Or I'm still doing a mistake somewhere.
I didn't understand what you wanna get.
If you get time as EPOCH time, you don't have information about time zone where this time stamp was made. So, you should know time zone offset and + or minus this seconds from this time stamp.
But I think the best way to use ISO 8601 format for time stamp, it's easy to convert to any timezone what you need
for example, this code convert ISO data to local time or return current time depends on locale timezone
private long time2LocalTimeZone (String date){
//"2016-07-29T23:07:45.120+00"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
try {
return sdf.parse(date).getTime();
} catch (ParseException e){
return (new Date()).getTime();
}
}
this code count your offset from GMT timezone and convert to epoch time depends on locale timezone
private long time2LocalTimeZone (long date){
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;
return date + offsetFromUtc;
}
Related
I have the datetime in the format like
2016-12-26 02:54:20 -0500
Now I need to convert this time to the device located time format.
If the device used in the Singapore then the time should convert automatically to the Singapore time zone, if device is in India then automatically it should convert to Indian time zone.
Please help to get the solution, Thanks in advance.
Use this method
public String convertTimeZone(String date) throws ParseException {
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
inFormat.setTimeZone(TimeZone.getDefault());
Date toConvert = inFormat.parse(date);
date = inFormat.format(toConvert);
return date.toString();
}
What does this date means: 1427856000472?
I have got this date from cursor.getString(4).
I need the receiving date of the message, but its format is not clear to me.
Kindly help, and thank you very much for sharing your knowledge.
Its in time in milliseconds .. you need to convert them to proper time.. following thing is what i followed.. it will return date month and year
where timestamp is equal to your time .. eg 1427856000472
final Calendar cal = Calendar.getInstance();
java.text.DateFormat df= DateFormat.getMediumDateFormat(mCtx);
java.text.DateFormat df1=DateFormat.getTimeFormat(mCtx);
String date = (df.format(timeStamp).toString());
String time=df1.format(timeStamp);
cal.setTimeInMillis(timeStamp);
int messageYear=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
int dates=cal.get(Calendar.DATE);
Locale locale=Locale.getDefault();
String monthName=cal.getDisplayName(Calendar.MONTH, Calendar.SHORT , locale);
String date = monthName+" "+dates+"\n"+time;
That time is the time elapsed since epoch(The Unix epoch is the number of seconds that have elapsed since January 1, 1970).
You can convert epoch time using this link:
http://www.epochconverter.com/
You can find the answer here in this post:
convert epoch time to date
Hope this helps.
How to find out the values of GMT for user for example it is +05:30 for India.
How do calculate this +05:30 value in Android ?
I need this because I am using a java library in my app which has a function with this +05:30 field and I want to generate this field by calculation so that I wont have to fill up individual values for countries.
This is what works awesome
public double getOffset(){
TimeZone timezone = TimeZone.getDefault();
int seconds = timezone.getOffset(Calendar.ZONE_OFFSET)/1000;
double minutes = seconds/60;
double hours = minutes/60;
return hours;
}
First get the epoch system time
System.currentTimeMillis()
Then use a date object, set the time zone to GMT and initialize with the long valye
dateObj.setTimeZone(TimeZone.getTimeZone("GMT"))
To get time in GMT use below function where dateInString is the value of date,and format is date format as yyyyMMddHH
public static long getDate(String dateInString,String format){
long date = 0;
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d = dateFormat.parse(dateInString);
date = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Use below method to get UTC :-
public int GetUnixTime()
{
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
int utc = (int)(now / 1000);
return (utc);
}
after you get UTC now compared it to the Epoch in this site http://www.xav.com/time.cgi.
see this below link :-
How can I get the current date and time in UTC or GMT in Java?
If you store a map between timezones and their GMT offsets in your app, you can call TimeZone.getDefault() to get the device's timezone and do a quick lookup to return the GMT offset. That way you don't have to rely on potentially tricky date/time calculations and can be sure you have the correct value.
I am building a basic logging utility class and would like to log the date and time down to the millisecond of when the log entry is created. I'm currently using:
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d )
which gives me '05-23 09:05:47'. I would like it to give me the milliseconds of when the log entry is created also and it does not appear that the DateFormat class supports millisecond retrieval.
Like the format "MM-dd hh:mm:ss:zzz" giving '05-23 09:05:47.447'.
Is it possible to do this using the DateFormat class (or a class like DateFormat)? I recognize it is possible to create another date removing the milliseconds part of this date and then subtracting the two and printing the difference but that's just silly. (:
Try this
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd hh:mm:ss SSS");
String dateToOutput = sdf.format(d);
While I think it's silly the DateFormat class doesn't allow easy formatted output of milliseconds, I realized that obtaining the milliseconds from the timestamp is actually quite easy. Every date object is representing a timestamp in milliseconds since 00:00 January 1, 1970 so the timestamp modulo 1000 gives the milliseconds.
I did
Date d = new Date();
String dateToOutput = DateFormat.format( "MM-dd hh:mm:ss", d );
dateToOutput += "." + d.getTime() % 1000;
which, while not ideal, works fine and gives me '05-23 09:05:47.447'.
I am using jxl api to read an excel file in android. When I get a date like "30/11/2012" from excel, the LabelCell output shows me date as "11/30/12".
1) I need to get the output in dd/MM/yyyy format when reading the excel file, because it exists that way in excel, so I wouldn't want to unnecessarily convert it into another format. How to do that ?
2) After reading in the excel column's date, I generate 2 variables, one which has excel date - 20 days (lets call it excelMinus20) and another excel date + 10 days (lets call it excelPlus10.
Now, I would like to check going further, if the current system date (smartphone's date) >= excelMinus20 and current system date <= excelPlus10.
How to do this whole thing using java.text.Date ? I tried using joda time as well, but it's too complicated to use. Please guide me at least in the right direction.
Thanks in advance
Omkar Ghaisas
To parse your date from text format:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse("30/11/2012");
More info : SimpleDateFormat doc
To substract days from your date:
public static Date substractDays(Date date, int days)
{
long millis = date.getTime();
long toSubstract = days * 1000 * 60 * 60 * 60 * 24;
// 1milli 1s 1m 1h 1d
return new Date(millis-toSubstract);
}
Adding some days would be the same, except replace - with +
To get back a String representation from a Date object:
DateFormat formatter = new SimpleDateFormat("...pattern...");
String formatedDate = formatter.format(date.getTime());
EDIT:
You could also do the Date adding/substracting with the method you suggested:
public static Date substractDays(Date date, int days)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -20 /*or +10*/);
return calendar.getTime();
}
If you want to check if a Date is in an interval, then:
public static boolean isInInterval(Date date, Date from, Date to)
{
return date.getTime()<to.getTime() && date.getTime() > from.getTime();
}