conversion date from string bring wrong value [duplicate] - java

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 9 years ago.
I'm using the following conversion from string to date
String attr = "2013-09-11"
Date date = null;
SimpleDateFormat parsedDate = null;
parsedDate = new SimpleDateFormat("yyyy-mm-dd");
try {
date = parsedDate.parse(attr);
} catch (ParseException e) {
}
Now the date have value Fri Jan 11 00:09:00 IST 2013
why is that? and how can I change it to bring appropriate value?

mm is for minutes, you're looking for MM for months.
parsedDate = new SimpleDateFormat("yyyy-MM-dd");
More info: SimpleDateFormat, in the Date and Time Patterns and Examples section .

Related

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

Convert String Date eg.(January 21, 2019) to Date (01/21/2019) in Java [duplicate]

This question already has answers here:
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Java string to date conversion
(17 answers)
Closed 3 years ago.
I'm trying to make my user input a date in "words" form, then print it as a date.
For example:
System.out.print("Enter Date: ")
String inDate = nextLine()
User will input January 21, 2019, then I will print it as 01/21/2019.
SimpleDateFormat fmt = new SimpleDateFormat("MMM dd, yyyy");
SimpleDateFormat fmtOut = null;
Date date = null;
try {
date = fmt.parse("January 21, 2019");
fmtOut = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(fmtOut.format(date))
} catch (ParseException e) {
e.printStackTrace();
}

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

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

Android convert UTC Date to local timezone [duplicate]

This question already has answers here:
java date parse exception while conveting UTC to local time zone
(5 answers)
Closed 7 years ago.
I get this date string from my API : "2015-12-07T14:11:15.596Z"
But this date is in UTC format and I want to convert it in local time, how can I do it ?
I tried this :
try
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.parse(this.created_at);
}
catch (ParseException e)
{
Log.e("Error Date at Whisp", e.getMessage());
return null;
}
But it return me this error :
Unparseable date: "2015-12-07T13:21:17.996Z" (at offset 10)
your Date Format pattern is wrong. Change to:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
For more informations see the javadoc of SimpleDateFormat
The T and the Z are not in your mask
Either
created_at = created_at.replace ("T", "").replace ("Z", "");
or modifiy your mask

Date and time in java not working [duplicate]

This question already has answers here:
DateFormat does not work?
(4 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I need to take date in the format date/month/year hours:minutes (time in 12 hour format) in java and I write the following code to save the date in datetoday. But it does not work for me. The output is shown in the following format Tue Nov 11 12:10:00 IST 2014. Can anyone help me regarding the issue? Thanks in advance.
Date datetoday;
void todaysdate() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/yyyy HH:mm");
String inputString2 = sdf.format(date);
try {
datetoday = sdf.parse(inputString2);
} catch (ParseException ex) {
System.out.println(ex.getMessage());
}
System.out.println(datetoday);
}
You can try this
new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
Use hh instead of HH to get 12 hours format

Categories