Date not parsing the month [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
What are the factors that make a date parsing not parsing the month correctly?
I have this very basic code :
DateFormat format = new SimpleDateFormat("DD/MM/yyyy", Locale.FRENCH);
Date date = format.parse(dateInterv);
dateInterv is a String with the value "14/06/2016" , and when I parse the date, date is in january. Even in debugger I can't see why it transforms 14/06/2016 into 14/01/2016.

Your String must be:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH);
Date date = format.parse(dateInterv);
because dd is for day of month and DD is for day of year

You can follow any of the following format from SimpleDateFormat.
SimpleDateFormat format = new SimpleDateFormat();
Date curDate = new Date();
format = new SimpleDateFormat("yyyy/MM/dd");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("MMMM dd HH:mm:ss zzzz yyyy",
Locale.ITALIAN);
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
DateToStr = format.format(curDate);
System.out.println(DateToStr);

I think you might wanna change it to this:
SimpleDateFormat formatter =new SimpleDateFormat("dd/MM/yyyy", Locale.French);
Date date = formatter.parse(dateInterv);
String formattedDate = formatter.format(date);

Related

I want to compare string date (dd.MM.yyyy) to current date?

I have a String date coming in form of dd.MM.yyyy. I want to compare if its a future date (today+1 day)
I am trying to convert the string into date and getting current date from SimpleDateFormat but when trying to convert the string date I am getting the output in "EEE MMM dd HH:mm:ss zzz yyyy" format.
String profileUpdateChangeDate = "31.01.2023"
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date changeDate = sdf.parse(profileUpdateChangeDate);
_log.info("changeDate===>>>"+changeDate);
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
String str = formatter.format(date);
_log.info("Currentdate-===>"+str);
How can I check if profileUpdateChangeDate is a future date?
You should be using the new java.time classes, so:
String profileUpdateChangeDate = "31.01.2023";
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate changeDate = LocalDate.parse(profileUpdateChangeDate, df);
LocalDate date = LocalDate.now();
System.out.printf("Is date %s in the future? %b%n", profileUpdateChangeDate, date.isBefore(changeDate));
You can compare the parsed date "changeDate" with the current date. If the "changeDate" is after the current date, then it is a future date.
String profileUpdateChangeDate = "31.01.2023";
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date changeDate = sdf.parse(profileUpdateChangeDate);
Date currentDate = new Date();
if (changeDate.after(currentDate)) {
System.out.println("profileUpdateChangeDate is a future date");
} else {
System.out.println("profileUpdateChangeDate is not a future date");
}

How to remove year in DateFormat follower locale?

I use DateFormat to show Day and Month. DateFormat supports MEDIUM, LONG, FULL all have a year. I want to remove year from this code and how can I achieve this
SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, locale);
sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
But I get an error with some locale like:
bo_CN : སྤྱི་ལོ་y MMMMའི་ཙེས་dད
vi: 'Ngày' dd 'tháng' MM 'năm' y
se_SE: d 'de' MMMM 'de' y
You can use SimpleDateFormat:
Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
String dateString = df.format(date);
Output:
01/21
Try with the below code
String shortDate = "dd.MM";
String mediumDate = "MMM dd";
String longDate = "MMMM dd";
String fullDate = "EEEE, MMMM dd";
Locale locale = new Locale("EN");
DateFormat df = new SimpleDateFormat(fullDate, locale); \\Use the required format here
String formattedDate = df.format(new Date());
I have removed the year part and created custom equivalent of
DateFormat.SHORT as shortDate
DateFormat.MEDIUM as mediumDate
DateFormat.LONG as longDate
DateFormat.FULL as fullDate

How to convert 'dd/mm/yyyy hh:mm:ss am/pm' to hh:mm:ss (without am or pm) in java?

I want the convert the string like 1/15/2014 9:57:03 AM or 1/15/2014 5:49:39 PM to 9:57:03 and 17:49:39
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.US);
Date date = format.parse("1/15/2014 9:57:03 AM");
format = new SimpleDateFormat("HH:mm:ss");
String dateString = format.format(date);
BTW, use Google next time
You should see this in the doc
doc SimpleDateFormat

How to parse date to EEE MMM dd HH:mm:ss zzz yyyy format?

I am getting date from database as 2013-05-03 00:20:29.0. I want to parse the date to EEE MMM dd HH:mm:ss zzz yyyy format, but when I am parsing it, I am getting this exception:
java.text.ParseException: Unparseable date: "2013-05-03 00:20:29.0"
My code is below:
String createdDate = "2013-05-03 00:20:29.0";
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date parseDate = format.parse(createdDate);
You have to adjust the Format pattern. Format must be yyyy-MM-dd HH:mm:ss.S
Try this
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your SimpleDateFormat pattern does not match createdDate. The format should be:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See the javadoc
As I understod you want to convert your date to a new format
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2013-05-03 00:20:29.0");
String str = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(date);
You can also try to get the date from the DB not as String but as java.sql.Date then the first step will be unnecessary
Here is an example:
String str_date= "29/02/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date dt = sdf.parse(str_date);
DateFormat df = new SimpleDateFormat("EEEE");
System.out.println(df.format(dt));

Help needed in formatting date in Java

Hi I have the following date as String format.
Input
2010-04-20 05:34:58.0
Output I want the string like
20, Apr 2010
Can someone tell me how to do it ?
Try this:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat outputFormat = new SimpleDateFormat("dd, MMM yyyy");
Date yourDate = inputFormat.parse("2010-04-20 05:34:58.0");
String formattedDate = outputFormat.format(yourDate);
You might want to try this:
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = inFormat.parse( "2010-04-20 05:34:58.0");
SimpleDateFormat outFormat = new SimpleDateFormat("dd, MMM yyyy");
System.out.println(outFormat.format( date));

Categories