I am unable to covert String values in The Java.util Date format.
This is my code
String string = "24-Nov-2012 14:21:56";
Date soldate = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss zzz").parse(string); // problem in this line
System.out.println(soldate);
Please help me to solve this issue.
new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss zzz")
You don't have that zzz - TimeZone part in your string. So, your format cannot parse your string. As it is different from the actual string format.
Just remove it: -
new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
SimpleDateFormat("dd-MMM-yyyy HH:mm:ss zzz") where zzz is the place holder for Timezone part for your date string.
So remove it if your date string is not going to have any timezone information and use this :
SimpleDateFormat("dd-MMM-yyyy HH:mm:ss")
See the link below for other options that yu can use with SimpleDateFormat
SimpleDateFormat javadoc
Related
When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.
2015-11-03T17:33:27
I currently get the above date as a String from a JSON call. The problem is, I have no idea how to parse the T in between the 3 and the 17. When I use SimpleDateFormat, shown below, I keep getting unparseable date. What exactly do I do with the T here?
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
messageDate = format.parse(recentMessage.getJSONObject("message_data").getString("sent_at"));
This should work
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date messageDate = format.parse("2015-11-03T17:33:27".replace("T", " "));
Also keep Timezone in mind when parsing the dates.
I want to know how I can convert a timestamp in the date format.
For example : 1415337782000 should be converted to "7 nov. 2014".
This is what I've tried so far :
Date date=new Date(location.date);
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
String dateText = df.format(date);
The timestamp is stored in location.date in long type.
Thanks in advance for your help.
You can pass a format to the SimpleDateFormat constructor, like this
SimpleDateFormat format= new SimpleDateFormat("d MMM, yyyy");
String dateText = format.format(new Date());
You can find the constants in this link:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
For better internationalization, you can define a string resource containing the desired format based on the location of the user. Something like
SimpleDateFormat format = new SimpleDateFormat(getResources().getString(R.string.YOUR_FORMAT));
Im working on an RSS reader software. I get items with their pubDate (publish date) values as string, convert them to Date object, and put them to my DB. However, when I check my DB, I saw some interesting values such as the date of tomorrow.
I research this situation and found that it is about time zone value Z. For example when I get "Mon, 26 May 2014 21:24:29 -0500", it becomes "2014-05-27 05:24:29", the next day !
All I want is to get dates in any timezone and convert them to date in common timezone, such as my country's.
Here is my code :
public static String convert(String datestr) throws ParseException {
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse(datestr);
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return resultFormatter.format(date);
}
And I use the method like that :
System.out.println(convert("Mon, 26 May 2014 21:24:29 -0500"));
The output is : 2014-05-27 05:24:29
Any idea ?
Since you haven't set a time zone, it's using your system's default.
Set a specific IANA time zone.
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
resultFormatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
return resultFormatter.format(date);
Looks like you passed a Date with timezone, but given a wrong format. If you are passing timezone like "-0500" you should rather use:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Remember that the system will always display the date using the current, default timezone (TimeZone.getDefault()) unless you override it by:
resultFormatter.setTimeZone(...)
This is working as expected. The date is converted as per your system's timezone.
Check the UTC offset of your system and replace it in the sample date string and look at the output.
For e.g: India is UTC+5:30
String datestr="Mon, 26 May 2014 21:24:29 +0530";
output:
2014-05-26 21:24:29
Alternate solution
If you don't want to consider the timezone of the input date string then simply truncate this information and remove zzz from pattern as well as shown in below code:
String datestr = "Mon, 26 May 2014 21:24:29 -0530";
datestr = datestr.replaceAll("\\s[-+](\\d+)$", ""); // truncate the timezone info if not needed
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); // remove zzz from the pattern
Date date = formatter.parse(datestr);
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(resultFormatter.format(date));
I am trying to convert a String value that is stored in a database,for example "2012-01-20", to be in the format January 20, 2012.
I have seen some examples, but they are using Date which works with SimpleDateFormat.
As an example here is one way I tried but the "try" always fails and the result is null
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate=null;
try {
convertedDate = df.parse(datePlayed);
} catch(ParseException e){
e.printStackTrace();
}
In short, you're not using the right format for parsing. You need to use two DateFormat instances; one for parsing and one for formatting.
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
Date convertedDate = parser.parse(datePlayed);
String output = formatter.format(convertedDate);
Date da = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E, dd/MM/yyyy !");
System.out.println("Update : " + ft.format(da));
You can change your date style do you want at: E, dd/MM/yyyy !
Good luck !
If the Date is read from a Database, then store it as a java.sql.Date. You can then use SimpleDateFormat on it, maybe after converting to java.util.Date. From the ResultSet object, you can extract Dates.
If what you meant is that you are given a date in text that was extracted from a DB by someone else and you are stuck with it. Try using:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(true);
convertedDate = df.parse(datePlayed.trim() );
Also try displaying the text you are parsing before you parse to make sure the datePlayed value is what you expect.
With parseInt, an extra space before or after the data will cause an error, so calling trim() removes extra spaces.