Java date format dd.mm.yyyy [duplicate] - java

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 1 year ago.
I have to parse String to Date format(dd.MM.YYYY).
Here is my code:
Date date = null;
try {
date = new SimpleDateFormat("dd.MM.yyyy").parse(regDate);
} catch (Exception e) {
System.out.println(e);
}
System.out.println(date);
But the output is: Tue Oct 12 00:00:00 EEST 2021. How can I change my code to print the date in the required format?

Try By using - or / in place of "."
date = new SimpleDateFormat("dd-MM-yyyy").parse(regDate);
or
date = new SimpleDateFormat("dd/MM/yyyy").parse(regDate);

Related

Format Date ddmmyy Java [duplicate]

This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Java string to date conversion
(17 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
display Java.util.Date in a specific format
(11 answers)
Java Android Calendar/DateFormat this format XXXXXXXX [closed]
(4 answers)
Closed 3 years ago.
I need convert String to Date with next format:
String date1 ="26032018";
// ddmmyyyy
I need:
032618
//mmddyy
I tried:
Date date2 = new SimpleDateFormat("ddmmyy").parse(date1);
But don't work
Output:
Thu Jan 03 00:26:00 CLST 2008
Any idea?
Works for me
String date1 ="26032018";
Date date2 = null;
try {
date2 = new SimpleDateFormat("ddMMyyyy").parse(date1);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("MMddyy").format(date2));
String date1 ="26032018";
String pattern ="MMddyyyy";
Date formatedDate = new SimpleDateFormat("ddMMyyyy").parse(date1);
String d = new SimpleDateFormat(pattern).format(formatedDate);

android parse string to date - incorrect date [duplicate]

This question already has answers here:
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Closed 4 years ago.
I've spent a lot of time and I still do not understand why this is happening. In the code below, the time changes correctly, the date is not.
String dtAll = mDate + " " + mTime;
Date dat = new Date();
#SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY HH:mm");
try {
dat = format.parse(dtAll);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("dat", String.valueOf(dat));
Log.d("dtAll", dtAll);
Results:
D/dat: Sun Dec 31 17:24:00 GMT+01:00 2017
D/dtAll: 20/09/2018 17:24
Will anyone help me?
Use year in format yyyy:
new SimpleDateFormat("dd/MM/yyyy HH:mm");

Why SimpleDateFormat does not apply specified date pattern? [duplicate]

This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I have following code to format a date, but the output does not match with the pattern.
try{
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
System.err.println(convertedDate.toString());
}catch(ParseException p){
System.err.println(p.getMessage());
}
Output
Sat Nov 01 00:00:00 EST 2014
I think you are questioning why you don't get the time, that's because your format doesn't include it. I suggest you use "yyyy-MM-dd'T'HH:mm:ss" to match your input and "yyyy-MM-dd" when you call DateFormat#format(Date),
try {
String date = "2014-11-1T12:14:00";
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(date);
System.out.println(convertedDate.toString());
// Per your comment you wanted to `format()` it like -
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(convertedDate));
} catch (ParseException p) {
System.err.println(p.getMessage());
}
For what you described below in our conversation, you can simply do this:
System.err.println((new SimpleDateFormat("yyyy-MM-dd")).format(convertedDate));

How to parse a Date+Time String [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
Using java I need to parse this 2014-08-31 13:53:42.0 to 31-AUG-14 01.53.42 PM
Start by doing some research into java.text.SimpleDateFormat which can be used to parse String values of a verity of formats into a java.util.Date.
You can then use another SimpleDateFormat to format the value to the format that you want, for example
try {
String in = "2014-08-31 13:53:42.0";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = sdfIn.parse(in);
System.out.println(date);
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MMM-yy hh:mm.ss a");
System.out.println(sdfOut.format(date));
} catch (ParseException ex) {
}
Which outputs
Sun Aug 31 13:53:42 EST 2014
31-Aug-14 01:53.42 PM

Android date format not correct? dd/MM/yyyy [duplicate]

This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 8 years ago.
I am trying to get a date to look like this format: 23/01/2014
This is what I currently have:
String testDate = test.getDate();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(testDate);
} catch (ParseException e) {
e.printStackTrace();
}
Log.i("DATE", convertedDate + "");
My output is: Mon Jul 07 09:40:54 GMT 2014 ?
Try this,
import android.text.format.DateFormat;
String dateString = (String) DateFormat.format("dd-MM-yyyy",new java.util.Date());
Check it with a simple toast message.
Toast.makeText(getActivity(),dateString,Toast.LENGTH_SHORT).show();

Categories

Resources