I am working on an app where I am saving date in currentTimeMillis() in sqlite database and when I read it I try to convert it to readable format. But date is always being converted to Jan 01, 1970.
Following is my code of Saving it to sqlite database:
long date = System.currentTimeMillis();
I then try to convert it to readable format in following way:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(attendance.getPicDate());
Date resultdate = new Date(attendance.getPicDate());
System.out.println(sdf.format(resultdate));
I am getting the following output:
Jan 17,1970 19:36
Edit1: attendance.getPicDate() value = 1432678159
Edit2: when I try to save value it is 1500378235812 but when I read it, it becomes above, My column in table is Integer.
1432678159 does convert to Jan 17,1970 19:36.
Try checking attendance.setPicDate().
I found my mistake I was using cursor.getInt(2) to fetch date from database but now I have corrected it to cursor.getLong(2) and it is working great now.
I suggest, you should save date and time in string format in database.
Calendar calendar=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String currentTime=sdf.format(calendar.getTime());
save that "currentTime" in your database.
Related
I'm developing an app in which I'm saving the date and time in Firebase and then retrieving it for the user to see.
What I want is I want user to see that time and date according to their local timezone. for ex - if the time and date saved in database is '2:07 PM' and 'Fri, Jun 24', then to the users in PST timezone, it should be shown as '1:37 AM' and 'Fri, Jun 24'.
Here's how I'm getting time and date:
DateFormat currentTime = new SimpleDateFormat("h:mm a");
final String time = currentTime.format(Calendar.getInstance().getTime());
DateFormat currentDate = new SimpleDateFormat("EEE, MMM d");
final String date = currentDate.format(Calendar.getInstance().getTime());
How can I achieve this?
Please let me know.
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
Date myDate = simpleDateFormat.parse("DateAndTimeToBeProcessed");
In my app i need to convert my current date into UTC format, i can successfully converted, now problem is i need 24 hours format check it out my below code
public static String CurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
LogUtil.d("new Date()" + new Date());
return df.format(new Date());
}
now my CurrentDate returns 1.45 but i need 13.45 how can i convert utc in 24 hours format?
i have search through google but dint get proper answer, all suggestions are most welcome
Thanks in advance
Changing the hour part at your SimpleDateFormat constructor call from hh to HH does the job:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Output:
2015-07-13 13:53:02
See also Table of Date and Time Patterns
As Jon Skeet said, check the String you pass on SimpleDateFormat() : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You need to replace "hh" with "HH" so it will become "yyyy-MM-dd HH:mm:ss"
Of course if you only need the hour and minute that should be "HH:mm"
I convert json date to human readable date but it shows less one then actual date. I
used this code to convert it:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Long timeInMillis = Long.valueOf(AttendanceModelList.get(position).getEmpdate());
calendar.setTimeInMillis(timeInMillis);
Date date=new Date(timeInMillis);
viewHolder.textemployeedate.setText(df.format(date));
Please help
You say as summary:
Your calendar date is one day less than expected when you try to
interprete a global timestamp of type java.util.Date as calendar
date.
This phenomenon can happen due to timezone effects or midnight change. Before viewing the technical solution, you have to ask yourself:
What is your default (system) timezone using TimeZone.getDefault()?
Do you run your code on a server which has not the expected timezone?
In which timezone do you wish to view the calendar date? (the timezone associated with your expected "actual" date)
How to specify the timezone?
java.util.Date d = ...; // from your JSON-timeInMillis?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tz = "Asia/Kolkata"; // or any other valid tz id
sdf.setTimeZone(TimeZone.getTimeZone(tz));
System.out.println(sdf.format(d));
I have a database which stores value in yyyy-MM-dd HH:mm:ss format.
When I retrieve values, I want them to be displayed in dd-MM-yyyy HH:mm AM/PM format. I cannot change the way I store them in the first place.
I am using a Cursor to get results from the database and then add them to a list, which is then used by listadapter to populate the listview.
Since Cursor returns a string how can I change the format of the returned value?
you can use SimpleDateFormat with the pattern like "dd-MM-yyyy HH:mm a".
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd-MM-yyyy HH:mm a");
System.out.println(mDateFormat.format(mDate));
You will want to do something like this:
//Retrieve date from database
SimpleDateFormat origFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stringDateFromDB = getDateFromDatabase();
//Store it as a date object
Date date = origFormat.parse(stringDateFromDB);
//Output it as a string that uses the new format
SimpleDateFormat newFormat= new SimpleDateFormat("dd-MM-yyyy HH:mm AM/PM");
String desiredDateFormat = newFormat.format(date);
Don't save date to database in any specific formats. you should save date in form of milliseconds and then use SimpleDateFormat to change it to any required format.
try as given below
Date c_date;
SimpleDateFormat outputFormat= new SimpleDateFormat("dd-MM-yyyy HH:mm a");
c_date=(Date)outputFormat.parse(Date_Database);
where
Date_Database= your date which is in String format which u have retraived from database
when i am print System.currentTimeMillis()
give me :
11-03 14:47:05.400: INFO/System.out(7579): date is :: 14475410111
What is the correct procedure to get entire date with time.?
To get current date and time in Android, You can use:
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
Output:
Current time => Thu Nov 03 15:00:45 GMT+05:30 2011
FYI, once you have this time in 'c' object, you can use SimpleDateFormat class to get date/time in desired format.
Final Solution:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sring formattedDate = df.format(c.getTime()); // c is Calendar object
System.out.println("========> formatted date => "+formattedDate);
Output:
========> formatted date => 2011-11-03 15:13:37
Date now = new Date(System.currentTimeMillis());
By the way : currentTimeMillis()
Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.
Yes This is the correct way to get the current time and date. afte that you need to convert it to desired format.
public static String getDateFromTimeStamp(String timeStamp){
return new Date(Long.parseLong(timeStamp)).toString();
}
Use the Date class.
(What makes you think that's wrong? You're asking for the time in milliseconds.)
Use SimpleDateFormat