Parsing date - yyyy-dd-mm hh:mm:ss [duplicate] - java

This question already has answers here:
java date format yyyy-mm-dd.hh.MM.ss.ms
(3 answers)
Closed 7 years ago.
I have a date string which is as follows:
2015-12-24 08:06:44
Now I have a parse function:
public static String parseDate(String date) {
Date oldDate = null;
try {
System.out.println("Unparsed: "+date);
DateFormat oldFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
oldDate = oldFormat.parse(date);
System.out.println("old Date parsed: "+oldDate);
DateFormat newFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);
Date result = newFormat.parse(oldDate.toString());
return result.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
The first System.out gives me:
Unparsed: 2015-12-24 08:06:44
The second one:
Sat Jan 24 08:06:44 GMT+05:30 2015
Whereas for the second one I have clearly mentioned the date format to be as:
new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Please advise on what's wrong here
I only want to get the month and day from the input string - Desired would be Dec 24 from the above sample date string.

Its MM for month not mm.
DateFormat oldFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
SimpleDateFormat doc

Related

convert date in android [duplicate]

This question already has answers here:
How to parse month full form string using DateFormat in Java?
(4 answers)
Closed 2 years ago.
I want to convert this date but parse exemption it should be like this result "2019-12-28 14:00:00"
try {
String strCurrentDate = "April 08 2020 4:24 AM"
SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
Log.d("datessscc", date);
} catch (ParseException e) {
e.printStackTrace();
}
Please change your SimpleDateFormat
Try This:
SimpleDateFormat input = new SimpleDateFormat("MMM dd yyyy hh:mm a");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date getAbbreviate=input.parse("May 22 2020 4:22 AM"); // parse input
Log.i("ouput_Date", "onCreate: "+output.format(getAbbreviate)); // format output
String date=output.format(getAbbreviate);
} catch (ParseException e) {
e.printStackTrace();
}

How to convert into dd-mmm-yyyy format to dd-mm-yyyy format in swing(core java)? [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
How can I change the date format in Java? [duplicate]
(10 answers)
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 3 years ago.
To convert dd-mmm-yyyy to dd-mm-yyyy format.
String dateofbirth = ((JTextField) dobcalender.getDateEditor().getUiComponent()).getText();//date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
tried these codes :
System.out.println(myFormat.format(myFormat.parse(dateofbirth)));
myFormat.format(myFormat.parse(dateofbirth));
showing error parseexception
Based on the format "24 Feb 2019"
SimpleDateFormat from=new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat to=new SimpleDateFormat("dd-MM-yyyy");
Date frm=from.parse("24 Feb 2019");
System.out.println(frm);//Sun Feb 24 00:00:00 IST 2019
System.out.println(to.format(frm));//24-02-2019
First, you need to parse the String to Date object. Then you need to convert the Date object to a new formatted String. Here is the sample code:
String dateofbirth = "09-10-2010"; //date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
// converting to Date object
Date date = myFormat.parse(dateofbirth);
SimpleDateFormat myFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
// converting Date object to new format
String formattedDate = myFormat1.format(date);
System.out.println(formattedDate); // prints: 09-Oct-2010
// Before Java8 , No thread safe , has to import multiple packages(util, text), throws checked Exception(ParseException)
System.out.println(oldDate); //21 Jul 2019
SimpleDateFormat oldPattern = new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat newPattern = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = oldPattern.parse(oldDate);
String newDate = newPattern.format(date);
System.out.println(newDate); //21-07-2019
} catch (ParseException e) {
// Exception handling message/mechanism/logging as per company standard
}
// In Java8, Thread Safe, Immutable, throws unchecked Exception(DateTimeParseException)
DateTimeFormatter oldPattern8 = DateTimeFormatter.ofPattern("dd MMM-yyyy");
DateTimeFormatter newPattern8 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate datetime = LocalDate.parse(oldDate, oldPattern8);
String output = datetime.format(newPattern8);
System.out.println(output); //21-07-2019

How to get a required Date format using SimpleDateFormat [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
How to parse a date? [duplicate]
(5 answers)
Closed 5 years ago.
My method is :
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
String date = null;
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
try{
Date date3 = df.parse(currentDate);
df.format(date3);
System.out.println("date3 " +date3);
Date previousDate = DateUtils.addDays(date3, variant);
date = previousDate.toString();
return date;
}catch (Exception e){
}
return date;
}
Note : currentTime variable always have the value like "18/12/2017"
I'm expecting result of date in dd/mm/yyyy format. but it always gives "Wed Jan 18 00:12:00 IST 2017" like this.
Run Time Results :
currentDate 18/12/2017
date3 Wed Jan 18 00:12:00 IST 2017
You should return the formatted date, not the toString() of the date. Try this:
Date previousDate = DateUtils.addDays(date3, variant);
return df.format(previousDate);
df.format simply returns a String representation of the Date with the format applied, therefore the line you have in your code has no effect.
Try changing you code to use the formatted output instead:
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date3 = df.parse(currentDate);
System.out.println("date3 " + df.format(date3));
Date previousDate = DateUtils.addDays(date3, variant);
return previousDate.toString();
}
Also - its bad to catch Exception, so you should remove that.
We should always try to find out the more convenient way so that we can further use it. In that case, we can use below method:
public String dateString(String input) {
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
String formattedDate = "";
try {
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
formattedDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
You can easily call this method like below:
Date date3 = df.parse(currentDate);
df.format(date3);
String input = date3.toString();
String requiredDate = dateString(input);
System.out.println("requiredDate: "+ requiredDate);
Which returns the output like below:
requiredDate: 18/12/2017

Change date format dd-MM-yyyy to yyyy-MM-dd in Java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I was trying to convert date string 08-12-2017 to 2017-12-08(LocalDate). Here is what I tried-
String startDateString = "08-12-2017";
LocalDate date = LocalDate.parse(startDateString);
System.out.println(date);
Also tried using formatter, but getting same result, an DateTimeParseException.
How can I get an output like 2017-12-08, without getting an exception?
Try this (see update below)
try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}
Update - Java 8
String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));
First you have to parse the string representation of your date-time into a Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");
Then you format the Date object back into a String in your preferred format.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);

How to Convert String in format yyyy-MM-dd to Date in same format in Java [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 7 years ago.
I want to convert my String in format 2015-09-07 to Date in format 2015-09-07. While parsing the Date is getting in different format. I need the result Date in same format what the String is.
Thanks&Regards
Sony K Koshy
Try like this":
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = "2015-09-07";
Date dt = sdf.parse(str);
System.out.println(dt);
Also refer: SimpleDateFormat for details.
Also you can create a generic function like this
private Date parseStringToDate(String dt, String format) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dt);
}
Using SimpleDateFormat:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = "2015-09-07";
Date date = simpleDateFormat .parse(dateString);
System.out.println(date); //Mon Sep 07 00:00:00 CEST 2015
public class StringToDate {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("y-MM-dd");
String stringDate = "2015-09-07";
try {
Date date = dateFormat.parse(stringDate);
System.out.println(date); // it will display Mon Sep 07 00:00:00 IST 2015
System.out.println(dateFormat.format(date)); // it will display 2015-09-07
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Categories