Android epoch wrong time - android

By using the below code i get 43753-07-31T04:40:44-0500
long installed = 1318562444444;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = formatter.format(new Date(installed * 1000L));
HELP :(

Installed is already in milliseconds.

Try this..
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Related

How to return current date in format "YYYY-mm-dd" as a Date datatype?

I know there are many ways to solve this but I am looking for a simple one line solution.
Typically it would be
new SimpleDateFormat("yyyy-MM-dd").format(new Date())
However this returns a string and I need an object of the Date type.
Using the GregorianCalendar class is okay too since it can return a Date type.
thanks
Try it out in this way:
String strDate = sdf.format(cal.getTime())
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat();
sdf1.applyPattern("yyyy-MM-dd");
Date date = sdf1.parse(strDate);
Date dateObj = new SimpleDateFormat("yyyy-MM-dd").parse( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
Date d = (new SimpleDateFormat("yyyy-MM-dd")).parse("2011-12-13");
or
Date now = new Date();

Android how to convert string into datetime format?

I have in String variable this ... "2015-01-12 19:00:00" So I want to convert to DateTime value , I have tried with this....
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss");
Date d = dateFormat.parse(fecha);
But I can't compile, I have gotten this message ... cannot convert from java.util.Date to java.sql.Date
How fix it?
The pattern is actually wrong, try this
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = dateFormat.parse(fecha);
Check if you have imported java.sql.Date instead of java.util.Date.

android SimpleDateFormat("dd/mm/yyyy") not working well

I have this function
private void setDateInTopBar() {
Date bodyPartDate = DataManager.instance().getSelectedBodyPart().getPublicationDate();
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String formattedDate = format.format(bodyPartDate);
TextView dateTxt = (TextView) findViewById(R.id.txtImageDate);
dateTxt.setText(formattedDate);
}
but my month is 0, when day and year is working well, what's the problem?
Replace:
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
With:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
as mm referes to minutes.. check http://developer.android.com/reference/java/text/SimpleDateFormat.html
See http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
lower-case m is for minutes.
Try: "dd/MM/yyyy"
I should set MM not mm here
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
mm means Minutes, So you shouldn't use it for Month, change mm to MM.
Your code must be like this:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Instead of this:
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
You can read all about these letters here

get current date and time

I'm kind of stuck on date and time.
I want my program to create the date like this "20121217". The first 4 letters are the year, the second 2 letters are the month and the last 2 are the day. year+month+day
The time is "112233" hour+minute+second
Thanks for your help!
That's a formatting issue. Java uses java.util.Date and java.text.DateFormat and java.text.SimpleDateFormat for those things.
DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd hhmmss");
dateFormatter.setLenient(false);
Date today = new Date();
String s = dateFormatter.format(today);
You can do something like this:
Calendar c = Calendar.getInstance();
String date = c.get(Calendar.YEAR) + c.get(Calendar.MONTH) + c.get(Calendar.DATE);
String time = c.get(Calendar.HOUR) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND);
Change any specific format of time or date as you need.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
currentDateandTime = sdf.format(new Date());
For date:
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String strDate = df.format(new Date());
For time:
DateFormat df = new SimpleDateFormat("hhmmss");
String strTime = df.format(new Date());
This works,
String currentDateTime;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentDateTime = sdf1.format(new Date());
What you're looking for is the SimpleDateFormat in Java... Take a look at this page.
Try this for your need:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss");
Date parsed = format.parse(new Date());
System.out.println(parsed.toString());
You can use following method to get current time
/**************************************************************
* getCurrentTime() it will return system time
*
* #return
****************************************************************/
public static String getCurrentTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime());
}// end of getCurrentTime()

getDate() does not return the exact date that system have?

i am implementing custom listview in android with image, text and date but when i call getDate method it will work but return 1/1/1970 instead of showing my system date?
You can just use new java.util.Date() and it will contain the actual date. You can also use Calendar.getInstance() or long time = System.currentTimeMillis(); new Date(time);
FYI,
getDate() => This method is deprecated. And it returns the day of the month.
You can use:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // you can mention any format which you want
String currentDateandTime = sdf.format(new Date());
Use this:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND)
android calender documentation
here is the complete solution.where you can get the date and also access it in the form of string.
private final static String getDateTime() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
Log.i("Log", "date is: "+df.format(new Date()));
return df.format(new Date());
}
you can try this
DateFormat date = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
Log.i("Log", "date is: "+date.format(new Date()));
date.format(new Date());
use this:
long dtMili = System.currentTimeMillis();
String date = new java.text.SimpleDateFormat("dd-MM-yy").format(new java.util.Date(dtMili));

Categories

Resources