I have string in 'MM/dd/yyyy' format and want to convert it into 'dd-MM-yy'.
e.g. '04/01/2012' should be converted into '01-Apr-12'
Can anyone please suggest how to get this?
SimpleDateFormat currentFormat = new SimpleDateFormat("mm/dd/yyyy");
SimpleDateFormat newFormat = new SimpleDateFormat("dd-mm-yy");
String dateInOldFormat="04/01/2012";
Date temp = currentFormat.parse(dateInOldFormat);
String dateInNewFormat= newFormat.format(temp);
i think things are pretty simple from here on...
You can use the Date class in Java. Parse a string with your format, and then output using a different format.
Below is example of date conversion... For your program, do changes accordingly...
String dateStr = "Thu Jan 19 2012 01:00 PM";
DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse( dateStr );
} catch ( ParseException e ) {
e.printStackTrace();
}
String formattedDate = "";
if( date != null ) {
formattedDate = writeFormat.format( date );
}
System.out.println(formattedDate);
Output is 2012-01-19 13:00:00
Good Luck
Related
"Mar 10, 2016 6:30:00 PM" This is my date and I want to convert this into "10 Mar 2016". Can I use SimpleDateFormat in android. I am not getting the exact pattern to convert it. Please help and thanks in advance
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("Some Pattern for above date");
Date newDate=spf.format(date);
spf= new SimpleDateFormat("dd MMM yyyy");
String date = spf.format(newDate);
Will this steps work? If yes, can someone please give me a pattern of that format? Thanks in advance.
This is modified code that you should use:
String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("dd MMM yyyy");
date = spf.format(newDate);
System.out.println(date);
Use hh for hours in order to get correct time.
Java 8 and later
Java 8 introduced new classes for time manipulation, so use following code in such cases:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a");
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println(dateTime.format(formatter2));
Use h for hour format, since in this case hour has only one digit.
conversion from string to date and date to string
String deliveryDate="2018-09-04";
SimpleDateFormat dateFormatprev = new SimpleDateFormat("yyyy-MM-dd");
Date d = dateFormatprev.parse(deliveryDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE dd MMM yyyy");
String changedDate = dateFormat.format(d);
You can use following method for this problem. We simply need to pass Current date format, required date format and Date String.
private String changeDateFormat(String currentFormat,String requiredFormat,String dateString){
String result="";
if (Strings.isNullOrEmpty(dateString)){
return result;
}
SimpleDateFormat formatterOld = new SimpleDateFormat(currentFormat, Locale.getDefault());
SimpleDateFormat formatterNew = new SimpleDateFormat(requiredFormat, Locale.getDefault());
Date date=null;
try {
date = formatterOld.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
result = formatterNew.format(date);
}
return result;
}
This method will return Date String in format you require.
In your case method call will be:
String date = changeDateFormat("MMM dd, yyyy hh:mm:ss a","dd MMM yyyy","Mar 10, 2016 6:30:00 PM");
You should parse() the String into Date and then format it into the desired format. You can use MMM dd, yyyy HH:mm:ss a format to parse the given String.
Here is the code snippet:
public static void main (String[] args) throws Exception
{
String date = "Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
Date newDate = spf.parse(date);
spf = new SimpleDateFormat("dd MMM yyyy");
String newDateString = spf.format(newDate);
System.out.println(newDateString);
}
Output:
10 Mar 2016
For the sake of completeness, here is the modern version. This is for anyone reading this who either uses Java 8 or later or is happy with a (good and futureproof) external library.
String date = "Mar 10, 2016 6:30:00 PM";
DateTimeFormatter parseFormatter
= DateTimeFormatter.ofPattern("MMM d, uuuu h:mm:ss a", Locale.ENGLISH);
DateTimeFormatter newFormatter
= DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH);
date = LocalDateTime.parse(date, parseFormatter).format(newFormatter);
System.out.println(date);
This prints the desired
10 Mar 2016
Please note the use of explicit locale for both DateTimeFormatter objects. “Mar” and “PM” both are in English, so neither the parsing nor the formatting will work unless some English-speaking locale is used. By giving it explicitly we are making the code robust enough to behave as expected also on computers and JVMs with other default locales.
To use the above on Android, use ThreeTenABP, please see How to use ThreeTenABP in Android Project. On other Java 6 and 7 use ThreeTen Backport.
You need to use SimpleDateFormat class to do the needful for you
String date = "Your input date"
DateFormat originalFormat = new SimpleDateFormat("<Your Input format here>", Locale.US)
DateFormat targetFormat = new SimpleDateFormat("<Your desired format here>", Locale.US)
Date Fdate = originalFormat.parse(date)
formattedDate = targetFormat.format(Fdate)
public static String formatDate(String fromFormat, String toFormat, String dateToFormat) {
SimpleDateFormat inFormat = new SimpleDateFormat(fromFormat);
Date date = null;
try {
date = inFormat.parse(dateToFormat);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat(toFormat);
return outFormat.format(date);
}
Use:
formatDate("dd-MM-yyyy", "EEEE, dd MMMM yyyy","26-07-2019");
Result:
Friday, 26 July 2019
How can I format a :
Tue May 21 00:00:00:00 GMT +200 14 <--- Tue May 21 00:00:00:00 GMT
+200 2014
i tried :
StringBuilder myName = new StringBuilder(datum);
myName.setCharAt(datum.length()-4, '2');
myName.setCharAt(datum.length()-3, '0');
Date date= null; DateTimeFormat.getFormat("-- idk ----").parse(myName.toString()); Window.alert(myName.toString());
but i dont know how to define the same date format as the Date class
i think this isnt a good solution is there a better?
I suppose that datum is a Date object.
So, just do:
DateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss:SS z yyyy");
System.out.println(df.format(datum));
More info: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
// getFormatedDate("in which pattern you are sending date", "how we want", date in string form);
example : formatedDate = getFormatedDate("yyyy-MM-dd", "ddMMyyyy", "2014-05-21");
private String getFormatedDate(String baseFormat, String reqFormat, String dateStr) {
String formatedDate = null;
try {
DateFormat fromFormat = new SimpleDateFormat(baseFormat);
fromFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat(reqFormat);
toFormat.setLenient(false);
java.util.Date date = fromFormat.parse(dateStr);
formatedDate = toFormat.format(date);
} catch (ParseException ex) {
}
return formatedDate;
}
You have to use date formatter.
Your date formatter should be
DateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss:SS z yyyy");
Then use this formatter to get your desired date.
Just do:
newDate = dateFormat.format(yourOldDate);
Hope this will works.
Get more from:
http://www.mkyong.com/java/java-date-and-calendar-examples/
and http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Thanks.
Response from jsp is coming in this format: "mm/dd/yyyy hh:mm", and I want to convert to db format "yyyy-mm-dd hh:mm:ss".
I tried this code :
public java.sql.Date getdateFormat(String datestring) throws ParseException {
String datestr = "";
try {
java.util.Date date = new SimpleDateFormat("mm/dd/yyyy hh:mm a",
Locale.ENGLISH).parse(datestring);
atestr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
} catch (Exception e) {
e.printStackTrace();
}
return getDateFromString(datestr);
}
public java.sql.Date getDateFromString(String string) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date finalDate = null;
try {
finalDate = sdf.parse(string);
} catch (Exception e) {
e.printStackTrace();
}
return new java.sql.Date(finalDate.getTime());
}
A common mistake in using SimpleDateFormat is skip the documentation and assume that is knows when mm means months and when mm mean minutes. It doesn't. mm only means minutes. If you want months use MM Also only use a if you expect AM/PM and only use hh for 12 hour clocks. I would expect your format should read
MM/dd/yyyy HH:mm
and your output
yyyy-MM-dd HH:mm:ss
BTW You shouldn't need to convert to a String to use JDBC. Using a Date is faster and less error prone.
If all you want is the Date then you do not need to do
atestr=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
simply return the date as this stage.
A date does not have any formatting, it is basically a number.
I would basically do it like this
String dateInString = "20140611";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date toDate = sdf.parse(dateInString);
sdf = new SimpleDateFormat("yyyy-MM-dd");
String tmpStr = String.format(sdf.format(toDate));
System.out.println(tmpStr);
We can get the date in the following way
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parseDate = dfm.parse(datestring);
Now the parseDate is in the format "yyyy-MM-dd HH:mm:ss"
I am using this functions to convert date in display format. But return wrong resopnse.
date = converDateFormate("yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy hh:mm a","2013/11/28 12:23:28");
Wrong Response: 11/28/2013 12:23 **AM**
Right Response: 11/28/2013 12:23 **PM** (how can I get this response)
public static String converDateFormate(String oldpattern,
String newPattern, String dateString) throws ParseException {
if (dateString != null) {
SimpleDateFormat sdf = new SimpleDateFormat(oldpattern);
Date testDate = null;
testDate = sdf.parse(dateString);
SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
String newFormat = formatter.format(testDate);
System.out.println("" + newFormat);
return newFormat;
} else {
return "";
}
}
Take a look at here at "Confusion at noon and midnight". If your input is in 24-hours format then you need to use HH:mm:ss in your pattern.
small mistake .
instead of
date = converDateFormate("yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy hh:mm a","2013/11/28 12:23:28");
use this
date = converDateFormate("yyyy/MM/dd HH:mm:ss", "MM/dd/yyyy hh:mm a","2013/11/28 12:23:28");
The old pattern needs to be changed to 24 hours format(HH:mm:ss).
So you have to change the line-
"date = converDateFormate("yyyy/MM/dd hh:mm:ss", "MM/dd/yyyy hh:mm a","2013/11/28 12:23:28");" to
"date = converDateFormate("yyyy/MM/dd HH:mm:ss", "MM/dd/yyyy hh:mm a", "2013/11/28 12:23:28");"
i am getting this error while i am trying to convert a string into date.
unparasable data
Below is my code:-
String str = "hello"
Second is missing at your parse String str. So, to parse it you should not include second format at SimpleDateFormat pattern. Also correct the day and Month format. Look at the declaration of df
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
To know details of pattern, go through this docs.
Edit
String date2 = sdformatter.format(date1);// format method return String.
//Should declare with String
Full Code
String str = "25-Nov-2013 06:00 AM";
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
SimpleDateFormat sdformatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
Date date1=null;
try {
date1 = df.parse(str);
} catch (ParseException ex) {
ex.printStackTrace();
}
String date2 = sdformatter.format(date1);
System.out.println(date2);
According to str format you should write your SimpleDateFormat,
(25-Nov-2013 06:00 AM ---> dd-MMM-yyyy hh:mm a) and for
(25-Nov-2013 06:00:30 AM-----> dd-MMM-yyyy hh:mm:ss a)
will work
Try this
long newerdate = new Date().parse("25-Nov-2013 06:30 AM");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("dd-MM-yyyy hh:mm a");
String data = df.format(newerdate);
System.out.println(data);