is it possible to convert Sms table date value e.g. 1293457709636 (miliseconds) to meaningful Date time value.
just do
long ms = 1293457709636; // or whatever you have read from sms
Date dateFromSms = new Date(ms);
You can convert Sms table date in to meaningful Date time like this.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
long now = 1293457709636L;
calendar.setTimeInMillis(now);
Log.i("time", "time"+formatter.format(calendar.getTime()));
Related
I have timestamp in DataBase like this: 1472373943.
In my Adapter I would like set Date & Time to different TextView.
For Example:
How to separate the date and time? Thanks.
First, what is your input? In your question, it looks like 1472373943, So I don't know which is time, and date position in that string. For simple, let assume that you have a sample date and time as follows:
String date = "12/8/2012";
String time = "11:25 am";
So, to convert it. The simple way is
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
String date = "12/8/2012";
String time = "11:25 am";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try {
Date dt = df.parse(date + " " + time);
Calendar ca = Calendar.getInstance();
ca.setTime(dt);
System.out.println(ca.getTimeInMillis());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
You can do that by using two different SimpleDateFormatters on the same Date object, one for the date and the other for the hours. "YYYY/MM/DD" and the other is "HH:mm a", just for an example.
am c# developer and in c# you can use datetime.now to get this time as example but in android studio i do not know how to do that can you help me please .
not
I need just hour and minute thanks
You could use this example to get the current date:
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
}
Using either Date or Calendar gives the same result in this case. But if you need to set particular dates or do date arithmetic, use a Calendar. Calendars also handle localisation.
In Android you are programming in Java. Java Date provides the functionality you are looking for.
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
I am storing from_date and to_date column in the database as sdf (yyyy-MM-DD). And I want to check if todays date is between these two dates or not.
My query is:
String where = Alarm.COL_TIME+" = "+"'"+time+"' AND Date(from_date) <= Date ('"+ today +"') AND Date(to_date) >= Date ('"+ today +"')";
But it shows nothing.
Please neglect time
how to do this??
Lets say You have a String as a Date like You said "yyyy-MM-dd". Lets say it´s "2014-12-03". Then, parse it to a long value with:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
String todayString = "2014-12-03";
String fromString = "2014-12-01";
String toString = "2014-12-05";
Date todayDate = format.parse(todayString);
Long today = todayDate.getTime();
Date fromDate = format.parse(fromString);
Long from = fromDate.getTime();
Date toDate = format.parse(toString);
Long to = toDate.getTime();
With this You have all three long values and can compair:
if(today>fromDate&&today<toDate){
//then today is between these days
}
The following code will store today's date in your today variable, in yyyy-MM-dd format:
// Imports
import java.text.SimpleDateFormat;
import java.util.Date;
// Above your "String where = Alarm..." assignment
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
I developed an application , in that i need to save the the event date and time. By default the time and date are in "America/Chicago" timezone format. Now, i need to convert them into user's device Time Zone format. but i got wrong format.
I did the following.
SimpleDateFormat curFormater1=new SimpleDateFormat("MM-dd-yyyy-hh:mm a");//10-23-2012-08:30 am
curFormater1.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
curFormater1.setTimeZone(TimeZone.getDefault());
current output:
TimeZone.getTimeZone("America/Chicago") is 10-22-2012-10:00 PM
TimeZone.getDefault() is 10-23-2012-08:30 AM
Required output
TimeZone.getTimeZone("America/Chicago") is 10-23-2012-08:30 AM
TimeZone.getDefault() is 10-23-2012-07:00 PM
Had the same trouble but found a way out.
TimeZone sets to the device's timeZone by default. To change this and to set it to a specific timeZone use the getRawOffset() property.
This method calculates the milliseconds from the current time. So you can add the milliseconds for your specified timeZone and subtract those for the default timeZone.
When I tried to change it to timeZone 'GMT_ID'
values.put(CalendarContract.Events.DTSTART, startDate.getTime() +TimeZone.getTimeZone(GMT_ID).getRawOffset() -TimeZone.getDefault().getRawOffset());
Hope this helps.
Some examples
Convert time between timezone
Converting Times Between Time Zones
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class TimeZoneExample {
public static void main(String[] args) {
// Create a calendar object and set it time based on the local
// time zone
Calendar localTime = Calendar.getInstance();
localTime.set(Calendar.HOUR, 17);
localTime.set(Calendar.MINUTE, 15);
localTime.set(Calendar.SECOND, 20);
int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);
// Print the local time
System.out.printf("Local time : %02d:%02d:%02d\n", hour, minute, second);
// Create a calendar object for representing a Germany time zone. Then we
// wet the time of the calendar with the value of the local time
Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);
// Print the local time in Germany time zone
System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
}
}
am working on countdown widget .The problem is explained below
'2012-07-04T15:00:00Z' - > '1341414000000'
'1341414000000' - > indicate 2012 july 4th 20:30
why this happend? . Am using joda
final String format = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();
String time="2012-07-04T15:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
// time.replace("Z","");
try {
Date date=df.parse(time);
long diff=date.getTime()-System.currentTimeMillis();
System.out.println("Date "+diff);
} catch (ParseException e) {
e.printStackTrace();
}
This seems like an old question, but I'll answer it anyway, since other people may find this.
In Joda there is a class for ISO 8601 formats, so instead of specifying the format manually you could use that class, as follows:
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;
String strDate = "2012-07-04T15:00:00Z";
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();
On the other hand, the problem that you seem to be having is related to the time zone. When you convert back from millis to date string, it gets converted using the local time zone. If you want to get the date as UTC, you should do the following:
import org.joda.time.DateTimeZone;
import org.joda.time.DateTime;
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
It will return 2012-07-04T15:00:00.000Z as expected. If you want to format it without the milliseconds, you can use the same formatter as before:
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
formatter.print(dt)
And it will return 2012-07-04T15:00:00Z.