I am using Java and I want to change my date format which will ignore initial zeros in date and time. Something like "2/2/2015 1:30 PM" instead of "02/02/2015 1:30PM"
you can use a SimpleDateFormat
SimpleDateFormat formatter = new SimpleDateFormat("d/MM/yyyy hh:mma");
String date = formatter.format(today);
see this link for the char codes:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Related
I want to convert String into Date in Java.
I have written following code for that:
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSSSSS");
Date date = dateformat.parse("2015-07-16 23:59:59.0");
dateformat.format(date);
After parsing I am getting following value for date : Fri Jan 16 23:59:59 IST 2015
I have tried many examples but didn't get proper solution.
Thanks in advance.
"mm" in the format string stands for minutes. You need to specify "MM" for the month part, or it defaults to January:
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
As you said you want to convert string to date then it will be received only in that format.
We cant format date in java we can format string representation of date using dateforamtter but if you are converting string to date it will be received in only that format that will be unformatted.
I need to convert date to this format 2015-02-27T15:14:13-06:00
I have tried with SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); , but this one generating as 2015-04-09T10:39:19-04
Tks
You can either use the new Yoda time which has ISO date formatters and parsers or you can use a SimpleDateFormat of "yyyy-MM-dd'T'HH:mm:ssZ" which gives you "ccyy-mm-ddThh:mm:ss+zzzz" and insert the extra colon manually.
Something like:
new StringBuilder(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(date)).insert(22,':').toString();
thanks oldcurmudgeon. I needed to set timezone and add your logic to insert : at 22nd position
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
fmt.setTimeZone( TimeZone.getTimeZone("US/Central"));
System.out.println("date to str:"+(new StringBuilder(fmt.format(new Date())).insert(22,':')));
I am getting the date time in "1/23/2013 6:57:17 AM" format and need to convert it into "2012-01-01T12:00:00" format. I could have solved the issue by using string functions and separating the date and time and dealing with them individually. But the problem is compunded by the fact that the date format is M/D/YYYY and even time has only h:mm:ss which means i cannot assume the number of characters before each delimiter.
I hope someone has dealt with something like this before. Thanks.
No, String functions are not the way to go.
I'd recommend using two DateFormat instances: one for the source format and another for the target format.
DateFormat source = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
source.setLenient(false);
DateFormat target = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
target.setLenient(false);
String dateAsString = "1/23/2013 12:00:00 AM";
Date d = source.parse(dateAsString);
System.out.println(target.format(d));
I am trying to do a simple String to Date conversion in Java. I am skimming the date off some logs and need to convert it to a date to do some processing. A date coming through will look like this:
2012-09-07 19:53:33
In my code when I try to convert this into a Date object I get a completely different date. My code looks like this:
String taskStart = "2012-09-07 19:53:33";
String dateFormat = "yyyy-MM-dd hh:MM:ss";
SimpleDateFormat dateFormat=new SimpleDateFormat(format);
Date taskStartDate = dateFormat.parse(taskStart);
The output I get is this:
Sat May 07 19:00:27 PDT 2016
How can I just simply convert my original date to the correct format??
You've specified MM (month in year) twice. The m's for "minute" must be lowercase.
If you're taking 24-hour based times, you need to specify HH in order to capture hours that are specified in the range of 0-23, as opposed to hh which expects AM/PM hours (hours in the range of 1-12 with an AM/PM specifier as part of the time string)
Finally, your example code doesn't define the format variable that you're passing into the constructor for your SimpleDateFormat object. In fact, you're using the variable name dateFormat twice and not defining the format variable at all - at least not according to the code that you included in the question.
So, your proper format string, I believe, should be...
"yyyy-MM-dd HH:mm:ss"
...and the full, proper code example would be:
String taskStart = "2012-09-07 19:53:33";
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormat=new SimpleDateFormat(format);
Date taskStartDate = dateFormat.parse(taskStart);
According to the documentation for SimpleDateFormat you should use an upper case "H" for hours of the format 0-23. Also, minutes are represented by a lowercase "m".
String dateFormat = "yyyy-MM-dd HH:mm:ss";
I think you should convert again the date output to String format using date formatter.format function:
Date formatter=new SimpleDateFormatter("yy-MM-dd HH mm ss")
String s=formatter.format(taskStartDate)
Try changing your dateFormat definition to this:
String dateFormat = "yyyy-MM-dd HH:mm:ss";
Note that the in your time, HH should be capitalized for 0-23 and mm should be lowercase for minute.
"yyyy-MM-dd hh:MM:ss";
MM and mm are for Month and Minute respectively.....
So it should be...
"yyyy-MM-dd hh:mm:ss";
I have My Database data in this format
18-NOV-10
I have to pass the same format into java.util.Date like this
Date date = new java.util.Date(dateformater);
so that the result of java.util.Date is like this 18-NOV-10
Is this possible ??
I tried this way
String strDate = "12-NOV-07";
SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MMM-yy");
Date date = sdfSource.parse(strDate);
System.out.println(date);
But i am getting the result as "Mon Nov 12 00:00:00 IST 2007 " which i want it only
12-NOV-07"
You can use java.text.DateFormat (actually SimpleDateFormat) to get you where you want to go, but maybe you shouldn't be storing the dates as strings in your database. It will do output and parsing.
SimpleDateFormat sdf =
new SimpleDateFormat("DD-MMM-YY");
Date parsed = sdf.parse(dateString);
See http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/
Once you get the Date, you can turn it into the format you want but it will be held in memory as a Date object. You can get it in the form you want using
String dateString = sdf.format(parsed);
As others have pointed out, you should probably store your dates as dates, not strings; nevertheless...
If you want to turn a Date back into a string in that format you can use the following:
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = new Date();
String dateStr = formatter.format(date); // Gives "22-May-11"
If you need MAY instead of May, just use toUpperCase() on the resultant string.
DateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
Date d = sdf.parse("18-NOV-10");
Try System.out.println(sdfSource.format(date).toUpperCase()); instead. The Date object will always have a time component to it; there is no way to "disable" that feature. What you can do instead is to ignore it in your calculations and display. If all Date objects you use are set to the same time of the day, then you can safely ignore the effect of the time component in your comparisons. If you look carefully, the time component of your Date object is set to midnight.