I want to get current Timestamp in this YYYY-MM-DDTHHmmssZ .For instance; Expected value : 2016-01-01%2000:00:00. I have tried various methods,One of them is below :
String timeStamp = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'").format(new Timestamp(System.currentTimeMillis()));
Still I am facing the error (didn't getting expected output which is 2016-01-01%2000:00:00) , Any help will be appreciate,thanks.
just use this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
String format = simpleDateFormat.format(new Date());
Log.d("MainActivity", "Current Timestamp: " + format);
just change the format in which you want current time stamp.
Thanks. :)
Your question is ambiguous but according to my understanding:
String timeStamp = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss'Z'")
You haven to set the timezone, like:
private static SimpleDateFormat df
= new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ssz")
df.setTimeZone(TimeZone.getTimeZone("GMT"));
OR
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(new Date())); //-prints-> 2015-01-22T03:23:26Z
If you want to learn more. please refer to:
https://svn.apache.org/repos/asf/commons/dormant/feedparser/trunk/src/java/org/apache/commons/feedparser/tools/ISO8601DateParser.java
Related
I want to enter the current date and time along with second in a text box ; How can I do that ?
The code what I have written is as below , but it is only entering the string value what I am passing in send keys , which is clearly indicating I am not in correct track .
Any suggestion ?
// Create object of SimpleDateFormat class and decide the format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy ");
//get current date time with Date()
Date date = new Date();
// Now format the date
String date1= dateFormat.format(date);
// Print the Date
System.out.println(date1);
collection_title.sendKeys("date1");
In collection_title.sendKeys("date1"); you are not using your variable date1, you are using the String "date1" - there should be no quotes around date1.
It should work like this:
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
collection_title.sendKeys(dateFormat.format(date));
Maybe you should try to use LocalDateTime.now() or Calender.getInstance() instead of new Date(), because the Date API methods are flawed and deprecated.
In PHP to convert a string to DateTime() its very very easy:
$dateTime = new DateTime("2013-12-11 10:109:08");
echo $dateTime->format("d/m/Y"); // output 11/12/2013
What is the equivalent in Java? I've seen a lot of questions in stackoverflow. I cant find a way to solve this problem.
My last try is:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ITALIAN);
return dateFormat.format(new Date(datetime)).toString();
This crash application. Android Studio tells me that Date(java.lang.String) is deprecated.
Can someone help me?
// First convert the String to a Date
String dateTime = "2013-11-12 13:14:15";
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ITALIAN);
Date date = dateParser.parse(dateTime);
// Then convert the Date to a String, formatted as you dd/MM/yyyy
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormatter.format(date));
You can let the parser / formatter take the timezone into account by using SimpleDateFromat.setTimeZone() if you have to deal with TimeZones that are not in your default locale.
try this
String time1="";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
calendar.setTimeInMillis(yourmilliseconds);
time1=sdf.format(calendar.getTime());
Yes As of JDK version 1.1, Date(java.lang.String) is deprecated and replaced by DateFormat.parse(String s).
Parse it like that:
SimpleDateFormat formatter =
new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY);
Calendar date = Calendar.getInstance();
try {
date.setTime(formatter.parse("12.12.2010"));
} catch (ParseException e) {
e.printStackTrace();
}
Have a look at my Android date picker example here.
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);
I am trying to in insert date into sqlite using:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String data = format.format(new Date(19900101));
System.err.println("Time is.."+format.format(19900101));
But i am getting 19700101 in log. i am getting same result even if i change the dates as 20000101 or any thing.
Any one suggest me why it happens?
the code is wrong
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date data = format.parse("19900101");
System.out.println("Time is.."+data );
when u use new Date(123) it expects some time in milliseconds i think
Change your code as for formatting current string to date :
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = (Date) format.parse("19900101");
SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fmconvertedDate =formats.format(convertedDate);
System.err.println("Time is.."+fmconvertedDate);
and output is :
Time is..1990-01-01 00:00:00
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);