How to convert date format in java? - java

I have a String a ="   December 2011   ";
There is a before and after it.
How can I Convert it to: String b = "2011-12-1"

If this is really all there is to it, use .replace():
String b = a.replace(" ", "");
Unlike what its name would let you believe since .replaceAll() exists, .replace() will actually replace all occurrences. The difference is that .replaceAll() expects a string which will be compiled as a Pattern as an argument.

Use SimpleDateFormat.
new SimpleDateFormat("yyyy-MM-d").format(new SimpleDateFormat("MMMMM yyyy").parse(str));

String a = " Dezember 2011 ";
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM yyyy");
a = a.replace(" ", "");
Date parse = sdf.parse(a);
SimpleDateFormat outSdf = new SimpleDateFormat("yyyy-MM-d");
String out = outSdf.format(parse);
System.out.println(out);

You use this code.It gives whatever you want to output.
You first remove &nbsp using replace function and after you have to just parse the your string.
try {
String a = " December 2011 ";
a = a.replace(" ", "");
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM yyyy");
SimpleDateFormat SdfParser = new SimpleDateFormat("yyyy-MM-dd");
String date =SdfParser.format(sdf.parse(a));
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}

Related

Date changing from one format to another format

I am trying to parse the date from one format to another format, but getting the parse exception. Please help me out on this issue.
String Orgdate= "2016-11-14T11:12:13";
java.util.Date tardate = null;
SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/mm/yyyy HH24:MI:SS");//Exception is in this line.
try {
targetdateformat = dateFormat2.parse(Orgdate);
} catch (ParseException e) {
e.printStackTrace();
}
Above code giving me date but different format,i am looking for date like below mention.
('14/11/2016 11:12:13')
Time format is 24 Hrs.
why not just do a simple hardcode to get the format you would like. For example...
String date = "2016-11-14T11:12:13";
String newDate = date.charAt(8) + "" + date.charAt(9) + "/" +
date.charAt(5) + "" + date.charAt(6) + "/" +
date.substring(0, 4) + " " + date.substring(11, 19);
System.out.println(newDate);
You need to parse to Date then format the date string to convert it into another format
String Orgdate= "2016-11-14T11:12:13";
java.util.Date tardate = null;
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
tardate = dateFormat2.parse(Orgdate);
System.out.println(tardate); // Mon Nov 14 11:12:13 UTC 2016
} catch (ParseException e) {
e.printStackTrace();
}
String formatted = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(tardate);
System.out.println(formatted); 14/11/2016 11:12:13
If you are using Java8 you can try below
LocalDateTime dateTime = LocalDateTime.parse("2016-11-14T11:12:13", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
String formattedDate = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")); // 14/11/2016 11:12:13
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date a = sdf.parse("2016-08-12T08:29:47");
sdf.applyPattern("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.toPattern()); // to see if new pattern applied
System.out.println(sdf.format(a)); // get output in desired format

Parse this date time string with regex

So I have the following string containing a date and time, which I need to parse
« by username on September 13, 2015, 08:34:02 am »
I have the following expression which seems to work in rubular.com but Java only collects September from it.
I would also like to have two groups, the date and the time. How can I do this?
January|February|March|April|May|June|July|August|September|October|November|December| [0-9]{2}, [0-9]{4}, [0-9]{2}:[0-9]{2}:[0-9]{2} am|pm
Thanks
One could try something like this
String in = "by username on September 13, 2015, 08:34:02 am";
//date parsing pattern
String s = "MMM d, yyyy, HH:mm:ss aaa";
SimpleDateFormat sdf = new SimpleDateFormat(s, Locale.US);
try {
//pattern to get rid of 'by username on'
String p = "\\w+\\s\\w+\\s\\w+\\s";
Date d = sdf.parse(in.replaceFirst(p, ""));
System.out.println(d);
} catch (ParseException e) {
e.printStackTrace();
}
Try this one.
((?:January|February|March|April|May|June|July|August|September|October|November|December)\s[0-9]{2},\s[0-9]{4}),\s([0-9]{2}:[0-9]{2}:[0-9]{2}\sam|pm)
Tested on your expression, it captures both date and time into separate groups.
If the date is always entered in exactly the same format you could use a function like the following. If you expect more spaces between the parts, then add \s+ (escaped as \\s+ in Java string).
public static Date findAndParseDate(String s) {
Date parsedDate = null;
String patternStr = "((January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{2}, [0-9]{4}, [0-9]{2}:[0-9]{2}:[0-9]{2} am|pm)";
Pattern p = Pattern.compile(patternStr);
Matcher m = p.matcher(s);
if (m.find()) {
String extractedDateTimePart = m.group(1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy, hh:mm:ss aa");
try {
parsedDate = simpleDateFormat.parse(extractedDateTimePart);
} catch (Exception ex) {
}
}
return parsedDate;
}

How to cut and replace String

I have a following string which I have to split and replace with "/" on some condition**
String date = "20131105";
I want to change these string to "2013/11/05"
Edit:
I mean variable date must be String not Date data type
Do like this
Date date = new SimpleDateFormat("yyyyMMdd").parse("20131105");
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);
Output
2013/11/05
Use the substring method.
date = date.substring(0, 4) + "/" + date.substring(4, 6) + "/" + date.substring(6, 8);
try this
String date = "20131105";
String date1=date.substring(0, 4);
String date2=date.substring(4,6);
String date3=date.substring(6,8);
System.out.println(date1+"/"+date2+"/"+date3);
output 2013/11/05
See you have many logics .. you can use any one..
for example answer from prabhakaran which is
Date date = new SimpleDateFormat("yyyyMMdd").parse("20131105");
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);
here you can do one change like this
Date date = new SimpleDateFormat("yyyyMMdd").parse(StringVaribale);
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);
here you are converting to date and then converting back to string
Another one is you can take a substring and add "/" into your string
in this logic you should take StringBuffer insteed of string. because this is having some extra feature.

How to format this string do a proper date format?

I'm very new to Java programming and I have string like this:
2013-03-15T07:23:13Z
I wish I could convert this into date format like:
15-03-2013
is that possible?
Thanks in advance.
Take the reference to this link
How can I change the date format in Java?
See the answer given by Mr. Christopher Parker
It has explained all your needs and it will provide you the easiest solution which is logically correct
Try this :
try {
DateFormat sourceDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
String strSourceDate = "2013-03-15T07:23:13Z";
Date targetDate = (Date) sourceDateFormat.parseObject(strSourceDate);
String strTargetDate = targetFormat.format(targetDate);
System.out.println(strTargetDate);
} catch (ParseException e) {
e.printStackTrace();
}
If the format of the input string is fixed, the simplest and the most expedient way of doing this would be with string manipulation:
String s = "2013-03-15T07:23:13Z";
String res = s.substring(8, 10)+"-"+s.substring(5, 7)+"-"+s.substring(0, 4);
It would spare you dealing with dates and calendars. Here is a demo on ideone.
java.text.SimpleDateFormat is what you need: SimpleDateFormat JavaDoc
You'll need one format to convert your input String into a Date using the parse() method, and then another to convert that Date into a String in your desired format using format().
If your application could be used internationally, don't forget to think about correctly localizing the output of the second function though. 03-11-2013 is March 11th in some countries, November 3rd in others.
Try this:
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("dd.MM.yyyy");
System.out.println("Current Date: " + ft.format(dNow));
It's output
Current Date: 15.03.2013
Using SimpleDateFormat
String strDate = "2013-03-15T07:23:13Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(strDate);
System.out.println("Today in dd-MM-yyyy format : " + date);
Hope it help you...

The date format issue from java

I get the date format from a joynet cloud api server:
2012-11-20T10:26:04+00:00"
However, I have no idea to handle the last segment +00:00, I have made the format except for +00:00
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = fmt.parse("2012-11-20T10:26:04");
Thanks for #Abu
I rewrite it to remove ":",
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String input = "2012-11-20T10:25:58+00:00";
String s1 = input.split("T")[0];
String s2 = input.split("T")[1];
String sep = null;
if (s2.contains("+")) {
sep = "+";
}
if (s2.contains("-")) {
sep = "-";
}
String s3 = s2.split("\\" + sep)[0];
String s4 = s2.split("\\" + sep)[1].replace(":", "");
String cleanDate = s1 + "T" + s3 + sep + s4;
Date date = fmt.parse(cleanDate);
System.out.println(date);
Remove that : inside the time zone part if you are not using java 7
and use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
See javadoc for SimpleDateFormat in Java 6
And
If you are using Java 7 then directly use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
See javadoc for SimpleDateFormat in Java 7

Categories

Resources