Date time object from webservice showing on Android textview with pattern? - java

Hi I have web service that return a date object like this as a return of the Json
"/Date(922312800000+0200)/"
However i need to show it on the textview in this pattern
"19.12.2011 16:15"
how can I convert that return to this pattern ?
Edit : Here is my code still giving java.lang.IllegalArgumentException
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yy");
String dateText = date.format(tempEntry.getCreatedDate());
Edit : Here is the code that work for me
String dateText = tempEntry.getCreatedDate();
String dateString = dateText.replace("/Date(", "").replace(")/", "");
String[] dateParts = dateString.split("[+-]");
Date dateFormat = new Date(Long.parseLong(dateParts[0]))

It appears to me that your Date is given in milliseconds from 1970, so, something like that:
// remove the unneeded information
String date = date.replace("/Date(", "").replace(")/");
String[] dateParts = date.split("[+-]")
//get the date represented by the given millis
Calendar c = Calendar.getInstance();
c.setTime(Long.parseLong(dateParts[0]);
// proceed with formatting to the desired date format.

You need to use: DateFormat.
Simple example:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String today = formatter.format(date);
textView.setText("Today : " + today);

Related

java date and time from timestamp

I am working on android app with achartengine where I am making a TimeSeries linegraph. I have stored all my variables inside an Arraylist. Since I need correct date object to insert in the time axis of my chart I am using,
int count = list.size();
Date[] dt = new Date[count];
for(int i=0;i<count;i++){
long a = Long.parseLong(list.get(i).get("time"));
dt[i] = new Date(a);
}
Here long a has the timestamp . With above piece of code. I am able to get dt as 09-Apr-2014 but I need the date to be shown as 09-Apr 12:55 . How can I do that,
I tried using the folllowing
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd HH:mm");
Date now = new Date();
String strDate = sdfDate.format(now);
But Since strDate is a string I cannot use it as dt[i] = strDate which will throws an error as one is Date and another is String.
How can I solve this ?
Thanks
You can solve it this way:
dt[i] = sdfDate.parse(strDate);
If you really just need the date strings, you can do this:
int count = list.size();
String[] dt = new String[count];
for (int i = 0; i < count; i++) {
long a = Long.parseLong(list.get(i).get("time"));
Date d = new Date(a);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
dt[i] = dateFormat.format(d);
}
Or, if you actually need the Date array, just format the dates on the fly as you need them.
Your question is misguided - you are showing how you create Date objects in the code, yet what you want to fix is how you show them.
The Date array will have dates precisely to the millisecond. The default toString() method of the Date objects shows only the day, that's why you're not seeing the time.
It is inherently the UIs responsibility to decide on the format of time that it is going to print, hence you should pass the Date array to the UI (or up to the point of printing) and format them there.
The DateFormat can do both (date to string representation and back):
DateFormat formatter = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss" );
Date to String:
Date date = new Date();
String sDate = formatter.format( time );
String to Date:
Date date = formatter.parse(sDate );
When you store the date, you should store it as precise as possible (milliseconds). For displaying it as a string, you can use whatever format you want.

Cast String Date to new format Java

I am trying to cast a date format which is string resultset from database to a standard format, but using simpledateformat gives following error:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at CopyEJ.CopyEJ.main(CopyEJ.java:113)
RROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:838]
With De-bug I found out variable time_stmp has value "2013-04-19 17:29:06" I want to cast to this:
yyyyMMddhhmmss
Here's code:
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
ResultSet rs_dt = cmd1.executeQuery(dt);
String time_stmp = null;
while (rs_dt.next())
{
time_stmp = rs_dt.getString(1);
}
StringBuilder ts = new StringBuilder( df.format( time_stmp ) );
What is the best way to achieve this?
Your Simple DateFormat has the wrong datepattern. You have to parse it to date with the pattern of you DB, then parse it back to String.
Try it this way:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
ResultSet rs_dt = cmd1.executeQuery(dt);
String time_stmp = null;
while (rs_dt.next())
{
time_stmp = rs_dt.getString(1);
}
Date d = null;
try {
Date d = df.parse(time_stmp);
} catch (ParseException ex) {
Logger.getLogger(Prime.class.getName()).log(Level.SEVERE, null, ex);
}
SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddhhmmss");
StringBuilder ts = new StringBuilder( df2.format(d) );
By the way:
If you want your output to be in 24h-Format, then you have to use the pattern yyyyMMddHHmmss
The problem is, format of the date retrieved from DB is yyyy-dd-MM HH:mm:ss and you are trying to parse it with the format yyyyMMddhhmmss
You can do something like this
String date = "2013-04-19 17:29:06";
Date d = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss").parse(date);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMddhhmmss");
System.out.println(outputFormat.format(d));
You need to use DateFormat.parse() here since the argument is of type String. DateFormat.format() on the other hand is used to format Date objects as String.
StringBuilder ts = new StringBuilder(df.format(df.parse(time_stmp)));
Also, it's recommended to save date/time data as a TimeStamp in database instead of a String.
Like it was mention you need to use
SimpleDateFormat.parse()
Also if your string is in the form
`2013-04-19 17:29:06`
You will want to construct your SimpleDateFormat for reading in like this
`yyyy-MM-dd HH:mm:ss` //make sure you use uppercase H for 24 hour times
Use SimpleDateFormat.parse() to convert a String to a Date.
Use SimpleDateFormat.format() to convert a Date to a String.
All together now:
SimpleDateFormat dbFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat stdFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String s = stdFormat.format(dbFormat.parse(input));
You can modify the formats to suit your needs.
Hi you can use below code to format date as of your choice (Ex: yyyy-MM-dd HH:mm:ss)
Calendar calendar = Calendar.getInstance();
Date calDate = calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Calendar Date: " + calDate);
System.out.println("Your Choice Date format (yyyy-MM-dd HH:mm:ss): "+dateFormat.format(calDate));
This will work
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd hhhh:mm:ss");
String time_stmp ="2013-04-19 17:29:06";
Date date=df.parse(time_stmp);
StringBuilder ts = new StringBuilder( df.format( date ) );
System.out.println(ts);

Convert date into specific format

I am working on Java. I have a date for example: 20120328.
Year is 2012, month is 03 & day is 28.
I want to convert it into yyyy-MM-dd format.
I have tried it but it is not working.
How to do it?
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
If you want other than "MM/dd/yyyy" then just change the format in SimpleDateFormat
Find some examples here.
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("01/29/02");
Take a look here. You will need to use the SimpleDateFormat class.
First you create the format using substring like
String myDate = "20120328";
String myConvertedDate = myDate.substring(0,4) + "-" + myDate.substring(5,6) + "-" + myDate.substring(7);
This produces the date as 2012-03-28
Then use simple date format to convert that into date if required.
You can use this one for Date formatting
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
And this one for getting TimeStamp
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Timestamp timestamp =timestamp=new Timestamp(System.currentTimeMillis());
String ourformat = formatter.format(timestamp);

How to get Date format like this in android?

How to get Date format like this?
Saturday,Dec 11,2011
Edited:
My code portion is like the following:
String outDate = "";
Date dT = new Date(year, mon, day);
SimpleDateFormat sdf = new SimpleDateFormat("EEE,MMM dd,yyyy");
outDate = sdf.format(dT);
and its output is `Sat,Dec 02,3911` when year = 2011,mon = 11,day = 2;
what is the reason of giving wrong month and year in output?
You can use SimpleDateFormat.
Try:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE,MMM dd,yyyy");
String text = formatter.format(...);
That will use the default locale - adjust accordingly for a different one.
Try to use this function
Date today=new Date();
public String getCurrentTime()
{
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MM,dd,YYYY");
String ClsCurrentDay = sdf.format(today);
return ClsCurrentDay;
}

Simple java date conversion

Having some troubles and can't find a quick answer..
Im trying to store a date within a string, and later fetch it to convert it back to a date.
However when storing the date using:
string tmp = new Date().toString();
And then trying to convert it back using
Date date = new Date(tmp);
I get the Exception type
java.lang.IllegalArgumentException
with my Android 2.2 device. Does work with 2.2 & 2.3 emus tho.
Any tips on what other way i can store and convert back?
You could use SimpleDateFormat with its methods parse() and format().
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
String tmp = sdf.format(new Date());
Date date = sdf.parse(tmp);
Do you need it to be a string? long is easier :)
do
long time = new Date().getTime();
Date date = new Date(time);
then you dont' have to parse
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// to string
String dateStr = formatter.format(new Date());
// to date
Date date = formatter.parse(dateStr);
use SimpleDateFormat as shown below.
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
//this will convert date into string.
String temp = formatter.format(currentDate.getTime());
//this will convert string into date format.
Date date=(Date)formatter.parse(temp);

Categories