android parse string to date - incorrect date [duplicate] - java

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");

Related

JAVA Date conversion from "hh:mm aa" does not list correct time [duplicate]

This question already has answers here:
Comparing two times in android
(4 answers)
Convert String to java.util.Date
(4 answers)
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
Closed 1 year ago.
I am a little confused about what I see with the following test code:
public class TheDateIssue {
public static void main(String[] args) {
String TIME_FORMAT = "hh:mm aa";
try {
Date theDate = new SimpleDateFormat(TIME_FORMAT).parse("13:15 pm");
System.out.println("theDate: " + theDate);
} catch (ParseException e) {
System.out.println("Exception while converting string to Date. \n" + e.getLocalizedMessage());
}
}
The time to be parsed is "13:15 pm" - but the sysout output lists the time (highlighted below). I was expecting either 13:15:00 PST or 01:15:00 pm PST
theDate: Fri Jan 02 **01:15:00 PST** 1970
What am I doing wrong? :(
Try this:
LocalTime time = LocalTime.parse("13:15")
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm a")));
Or:
ZonedDateTime time = ZonedDateTime.of(LocalDate.now(), LocalTime.parse("13:15"), ZoneId.of("America/Los_Angeles"));
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));
And if for some reason you are stuck with a date class, transform it somehow:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ssZ aa");
Date theDate = simpleDateFormat.parse("13:15:00 PST PM");
ZonedDateTime date = ZonedDateTime.ofInstant(theDate.toInstant(), ZoneId.of("America/Los_Angeles"));
System.out.println(date.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));
But you are right, it is weird behaviour. There are good reasons they created LocalDate etc, moving away from the Date class.

Java date format dd.mm.yyyy [duplicate]

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);

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();

SimpleDateFormat returns a strange value in java [duplicate]

This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 6 years ago.
I have a date which is stored in string format, under the following convention yyyy-mm-dd, and I want to get the month, so this is what I'm doing
String dateS = j.getString(key);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
if ("null".equals(dateS))
dateS = j.getString("created");
try {
cal.setTime(sdf.parse(dateS));
System.out.println("parsed: " + sdf.parse(dateS));
System.out.println("original: " + dateS);
System.out.println("Month: " + cal.get(Calendar.MONTH));
} catch (ParseException e) {
// null, for now don't do anything, but this should never happen
}
And this is the output:
parsed: Sun Jan 19 00:02:00 EST 2014
original: 2014-02-19
Month: 0
As you can see, it's not parsing it into the right format, what am I doing wrong? is new SimpleDateFormat("yyyy-mm-dd"); not the right way to do it?
Remember that 'm' means minutes and 'M' means months
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Categories

Resources