How to get the time when app launches in android - android

I want to store the app launch time in sqlite and use that time for the next launch in the functionality .. for that i am using
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String data = format.format(new Date(19900101));
System.err.println("Time is.."+format.format(19900101));
I stuck how to get the time dynamically in format of yyyy-MM-dd kk:mm:ss.Even though using this code i am getting 1970-01-01..Please suggest me right way to do this

Change your code to
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String data = format.format(new java.util.Date());
System.err.println("Time is.."+data);

You able to take time via this
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
OR change your code like this.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String data = format.format(new Date());
System.err.println("Time is.."+format.format());
Thanks.

Related

Sending DATE/TIMESTAMP from device to REST service

I am trying to send a date and time to my SQL oracle database through my REST service. However, the field in the SQL database is getting null.
Here is my code to get the date and time:
public void getCurrentDateandTime() throws ParseException {
Calendar c = Calendar.getInstance();
c.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentDateandTime = dateFormat.format(c.getTime());
CDAT = dateFormat.parse(currentDateandTime);
}
In my REST service, this field is specified as a date datatype, however my the temporal I am using is TIMESTAMP as I want to get the date and time.
When I use postman sending through test data like this:
"createdTimestamp": "2018-02-12T09:27:39"
It is being received and shown in my SQL database.
Why is the date I am sending from Android receiving null?
Try this buddy:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
And then send this formattedDate string. Make it string type and then parse your string as datetime object. Hope this works. Happy Coding :)
Most REST APIs stick with iso8601 format as a standard.
The java code would be like:
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
Then in Oracle to get back is:
to_timestamp('2018-02-14T00:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')

How to get Current Date and time of Dubai in Android?

I am getting current date of the device, but i want the date and time of city Dubai, Any one please help me in doing this.when the activity called the date and time should be displayed,
Any help will be appriciated. thank you..
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = format.parse("2011-03-01 15:10:37");
// => This time is in the user phone timezone, you will maybe need to turn it in UTC!
TimeZone tz = TimeZone.getTimeZone("Asia/Dubai");
format.setTimeZone(tz);
String result = format.format(parsed);
You can use TimeZone to pass to the SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Dubai"));

How to Compare system date with mydate in android 2.1?

in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date.
if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.
how can i achieve this is android.
Thanks in advance.
You can convert String to Date like this:
String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
And back to String
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));
And Date has before and after methods and can be compared to each other.
By the way there is also a library called Joda, you can also check it out.
Try this code:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
formattedDate = df.format(c.getTime());

how to get the timestamp values in new york time zone in android?

below is my android code by which I need to get the timestamp value(value is coming from web server) in New York timezone. Please help me out because system is converting time to server timezone.
long startTime = Long.valueOf(appObj.getString("Unix_timestamp"))*1000;
Date fromDateObj = new java.util.Date(startTime);
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("HH");
fromHour = Integer.valueOf(sdf.format(fromDateObj));
sdf.applyPattern("mm");
fromMin = Integer.valueOf(sdf.format(fromDateObj));
Thanks in advance....
You can specify desired timezone for SimpleDateFormat:
sdf.setTimeZone(TimeZone.getTimeZone("EST"));

How to get time format using country ISO code in android?

I am working on simple application to show the time in different formate. To show the time I am displaying ISO country code using this ISO code. Can I able to change time in that ISO country format?
I have written code as follows
TelephonyManager tMgr =
(TelephonyManager)getApplicationContext().getSystemService(
Context.TELEPHONY_SERVICE);
String iso = tMgr.getNetworkCountryIso();
Log.v("Device iso", "=======>" + iso);
String mbNo = tMgr.getLine1Number();
Log.v("mbNo", "=======>" + mbNo);
Here I am getting iso as US. Can I show the current system time format in US time format?
I have used Date class to show time as follows
DateFormat df = DateFormat.getTimeInstance();
systime = df.format(new Date());
It is displaying time in HH:MM:SS AM/PM format. I would like to display the above time as US format.
How can I display the time in US or any other format?
In your case I think you could use
Date systemDate = Calendar.getInstance().getTime(); // the current system time
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, Locale.US);
Date myDate = df.format(systemDate);
Or to use custom format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date myDate = sdf.format(systemDate);
if you have a string use parse
Date myDate = sdf.parse("29/04/1980 12:30:24");
DateFormat has a static method that can initalize a localized format.
I believe that you need to create your own method for US format. Try to create a class and make its super class a DataFormat class and simply add your method.

Categories

Resources