Java date pattern [duplicate] - java

This question already has answers here:
How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)
(5 answers)
Closed 7 years ago.
i have this string output 'Wed Apr 01 09:50:31 CEST 2015' can anyone tell me what pattern is that . i need it in order to use it in another function.
#Test
public void test() throws ParseException {
String input = "Wed Apr 01 09:50:31 CEST 2015";
String format = "DD MM hh:mm:ss YYYY"; // i have a wrong format
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println(week);
}
any help to figure out the correct format of date pattern in my case.

Solution to your problem is to override default Date locale using
SimpleDateFormat(String pattern, Locale locale) constructor:
DateFormat dateFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
dateFormat.parse("Tue Jul 13 00:00:00 CEST 2011");
System.out.println(dateFormat.format(new Date()));
Solution copy of How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)

Related

I can't convert this String to Date [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 4 years ago.
I want convert "Thu Jan 18 00:00:00 CET 2018" to "2018-01-18" -> yyyy-mm-dd.
But I get error -> java.text.ParseException: Unparseable date: "Thu Jan 18 00:00:00 CET 2018".
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
stringValue = String.valueOf(cell.getDateCellValue());
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(stringValue);
System.out.println(date);
break;
case Cell.CELL_TYPE_STRING:...
break;
}
Maybe is why I have cell.getCellType() and Cell.CELL_TYPE_NUMERIC deprecated ?
Seems your Locale is not in this format, try Locale.US, and you need use x for timezone, try:
String input = "Thu Jan 18 00:00:00 CET 2018";
DateFormat parser = new SimpleDateFormat("E MMM dd HH:mm:ss x yyyy", Locale.US);
Date date = parser.parse(input); // parse String to Date
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.format(date)); // format Date to String
Or you can use ZonedDateTime since java8:
String input = "Thu Jan 18 00:00:00 CET 2018";
ZonedDateTime zonedDateTime = ZonedDateTime.parse(input,
DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.US));
System.out.println(DateTimeFormatter.ISO_DATE.format(zonedDateTime.toLocalDate()));
If you don't specify a locale, then JVM will use the default locale of the system. If that also happens to be US English (or some other language where the abbreviation for January is "Jan"), then the results will be the same.
Please take a look at this thread : https://coderanch.com/t/491599/java/SimpleDateFormat-locale

Converting date object to yyyy-MM-dd hh:mm:ss [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
SimpleDateFormatter.parse giving output in different format than specified
(1 answer)
Closed 4 years ago.
I am trying to convert this date object Fri Sep 21 08:00:00 SGT 2018 to this format yyyy-MM-dd hh:mm:ss date object.
Date dt = sd1.parse(startTime);
logger.info(dt);
logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dt.toString()));
The first log statement works, it returns me Fri Sep 21 08:00:00 SGT 2018 but the second log statement does not work.
It throws me an error
unparseable date Fri Sep 21 08:00:00 SGT 2018
What am i doing wrong here? My end goal is to get the date object in yyyy-MM-dd hh:mm:ss format.
First you have to parse the date properly with :
new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
So this should work:
Date newDate = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(newDate.getTime());
System.out.println(strDate);
But I suggest you to go with LocalDateTime
LocalDateTime ldt = LocalDateTime.parse("Fri Sep 21 08:00:00 SGT 2018", DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS")));

How to convert string (Twitter4j format) to Java Date

I would like to extract date and year from the following string and convert it to a Data Object in Java.
Mon Jul 07 19:18:26 CEST 2014
How can I extract only date and year (in this case, 2014-07-07) from the text in a sophisticated way?
SimpleDateFormat s = new SimpleDateFormat("dd/MMM/yyyy");
String dateInString = "Mon Jul 07 19:18:26 CEST 2014";
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = s.parse(dateInString.split(" ")[2]+"/"+dateInString.split(" ")[1]+"/"+dateInString.split(" ")[5]);
System.out.println(new SimpleDateFormat("YYYY-MM-dd").format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This should work for you, I splitted your String, then put it to a date format and then formatted it the way you wanted it to be, assumed you wanted the months as the second parameter after the year, if thats not the case you can simply change the 'MM' to 'dd' and the 'dd' to 'MM'.
For Java 7 or below, use a SimpleDateFormat for parsing and formatting:
Locale dateLocale = Locale.US;
SimpleDateFormat inFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", dateLocale);
Date date = inFormat.parse("Mon Jul 07 19:18:26 CEST 2014");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", dateLocale);
System.out.println(outFormat.format(date));
Since Java 8, you can use DateTimeFormatter:
Locale dateLocale = Locale.US;
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", dateLocale);
TemporalAccessor date = inFormatter.parse("Mon Jul 07 19:18:26 CEST 2014");
DateTimeFormatter outFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
System.out.println(outFormatter.format(date));

Getting the Date format in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I am have this String from a Date object: Mon Mar 25 00:00:00 IST 2013
What is the String representation of the date format?
The code below should work fine.
public static void main(String[] args) throws Exception {
String target = "Mon Mar 25 00:00:00 IST 2013";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
Read the javadoc for the valid patterns.
String yourDate= "Mon Mar 25 00:00:00 IST 2013" ;
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date jDate = new Date(df.parse(yourDate).getTime());
System.out.println(jDate);

"Unparseable date" when trying to convert "16 Nov 2011 08:00" string to Date? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert a date String to a Date or Calendar object?
How can I convert "16 Nov 2011 08:00" to a Date object? I tried SimpleDateFormat but does not works, I get
java.text.ParseException: Unparseable date: "16 Nov 2011 08:00"
or will I have to split the string enter slashes and then give it a try ?
You problem with the SimpleDateFormat was probably an incorrect format string.
Here is the correct way:
DateFormat formatter = new SimpleDateFormat( "dd MMM yyyy HH:mm" );
Date date = formatter.parse( "16 Nov 2011 08:00" );
You will need to use SimpleDateFormat to set a date format then use .parse(String s) to convert your String to a Date, here's an example:
DateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm");
String theDateString = "16 Nov 2011 08:00";
Date myDate = format.parse(theDateString);

Categories