I am trying to convert string value to date but its not working
Date dat1;
System.out.println("String date is "+tdate);// this shows 20141015
try{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dat1 = dateFormat.parse(tdate);
}
catch(Exception e){
System.out.println("error");
}
tdate is String variable. The code did not show any error it runs fine but
it prints error of catch block and does not execute the code in try
please help thanks.
Use this
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Date date = formatter.parse(tdate);
The format does not match the way the date is written. Use this instead:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Related
I am getting response string 2018-10-03T09:00:36.845+0000
and i have to parse it for PrettyTime some min ago .
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
its not working, can you tell me Dateformat string to add in SimpleDateFormat ?
Here is My code
:
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
long time = sdf.parse(device_for_bottomviews.get(i).lastupdate).getTime();
PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
String ago = prettyTime.format(new Date(time));
lastupdate.setText(ago);
}
catch (Exception e)
{
e.printStackTrace();
lastupdate.setText(device_for_bottomviews.get(i).lastupdate);
}
exception i am getting
java.lang.IllegalArgumentException
at java.util.Date.parse(Date.java:633)
at java.util.Date.<init>(Date.java:272)
at com.test.busmanagement.MapActivity$DeviceAdapter.getView(MapActivity.java:721)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
You can use this format as pattern
yyyy-MM-dd'T'HH:mm:ss.SSSX
You don't need to quote the 'zone' part of the date format string, here is a corrected version:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
I am trying to get the current date in a Talend job and I am using this as my context variable:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
context.dateout = dateFormat.format(date);
System.out.println(context.dateout);
However, the type of the result is a String and not a Date.
How should I correct it?
Thank you very much!!
Try to do that according the following code:
String string = "2016-03-15";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
Date date = dateFormat.parse(string);
System.out.println(date);
} catch (ParseException ex) {
System.out.println(ex);
}
I don't know what your context.dateout means.
Note the difference between parse and format.
This is to create a string from a date:
dateFormat.format(date);
This is to create a date from a string:
dateFormat.parse(dateString);
I have a date in the format dd-mm-yyyy format(inside my db).I need to convert it to dd-mm-yy format.I retrieved date from db and stored it inside a string.
String date=doc.getString("date");
for example: 05-19-1990
to
05-19-90.
That 1990 is not fully needed only 90 is needed.Using split() in java i tried some thing but it wont helps me.Can anyone please help.Any help will be highly appreciated.......
Use DateFormat for that issue:
DateFormat df = SimpleDateFormat("dd-MM-yyyy");
DateFormat df1 = SimpleDateFormat("dd-MM-yy");
df1.format(df.parse(date));
Try below code
try {
SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yy");
Date d = sdf.parse("05-19-1990");
System.out.println(sdf.format(d));
} catch (ParseException ex) {
ex.printStackTrace();
}
Use two SimpleDateFormats, one to parse it, one to format it..
SimpleDateFormat in = new SimpleDateFormat("MM-dd-yyyy");
Date inDate = in.parse(date); // 05-19-1990
SimpleDateFormat out = new SimpleDateFormat("MM-dd-yy");
String newDate = out.format(inDate);
or
SimpleDateFormat out = new SimpleDateFormat("dd-MM-yy");
String newDate = out.format(inDate);
If you really want 19-05-90 as per the title of your question ;)
Check java.text.SimpleDateFormat for more details
Try this..
DateFormat df2 = new SimpleDateFormat("dd-mm-yy");
String formattedDate2 = df2.format(theDate);
I have a String in Java which is a date but is formatted like:
02122012
I need to reformat it to look like 02/12/2012 how can this be done.
With the following code I keep getting back java.text.SimpleDateFormat#d936eac0
Below is my code..
public static void main(String[] args) {
// Make a String that has a date in it, with MEDIUM date format
// and SHORT time format.
String dateString = "02152012";
SimpleDateFormat input = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
try {
output.format(input.parse(dateString));
} catch (Exception e) {
}
System.out.println(output.toString());
}
Use SimpleDateFormat.
SimpleDateFormat input = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(output.format(input.parse("02122012"))); // 02/12/2012
As suggested by Jon Skeet, you can also set the TimeZone and Locale on the SimpleDateFormat
SimpleDateFormat englishUtcDateFormat(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf;
}
SimpleDateFormat input = englishUtcDateFormat("ddMMyyyy");
SimpleDateFormat output = englishUtcDateFormat("dd/MM/yyyy");
System.out.println(output.format(input.parse("02122012"))); // 02/12/2012
This is the problem with the code in your edited question:
System.out.println(output.toString());
You're printing out the SimpleDateFormat, not the result of calling format. Indeed, you're ignoring the result of calling format:
output.format(input.parse(dateString));
Just change it to:
System.out.println(output.format(input.parse(dateString)));
Or more clearly:
Date parsedDate = input.parse(dateString);
String reformattedDate = output.format(parsedDate);
System.out.println(reformattedDate);
I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another