Converting a String of a Date to a Date - java

I'm having troubles converting a String of a date entered by a user to an actual Date that can be sent to a database.
Ultimately the user enters a date in the format of YYYY-MM-DD and it gets sent to the database. I'm trying this:
String date = "2015-03-02";
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
Date parsedDate = sdf.parse(date);
this is all it outputs
Sun Dec 28 00:00:00 CST 2014

Your format String should be yyyy-MM-dd; and something like
String date = "2015-03-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date parsedDate = sdf.parse(date);
System.out.println(sdf.format(parsedDate));
} catch (ParseException e) {
e.printStackTrace();
}
Output is
2015-03-02

Related

How change String to Date format

I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);

Parse yyyy-MM-DD String to Date yyyy-MM-DD in Android?

String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new 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();
}
}
}

How parser String to date formate

I have doubts about how parser a String (EditText) for an Date formate
I need sent to my DataBase in this format: 1991-04-01 00:00:00
But my code return in this format: Thur Apr 11 00:00:00 BTR 1991
Can you please help me in parse my EditText to Date in this format: 1991-04-01 00:00:00
follow my code:
Date newFormat = new Date();
String birthday = (mEditTextBirthday.getText().toString());
try {
newFormat = format.parse(birthday+" 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
mUser.setBirthday(newFormat);
Simply use
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
String birthday = mEditTextBirthday.getText();
Date = new Date();
try {
date = format.parse(birthday + " 00:00:00");
} [...]
Looks like you are using a wrongly defined SimpleDateFormat.
JavaDoc provides you with many examples.

How to convert java.util.Date to GMT format

I have a string "2014-07-02T17:12:36.488-01:00" which shows the Mountain time zone. I parsed this into java.util.date format. Now I need to convert this to GMT format. Can anyone help me??
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Object dd = null;
try {
dd=sdf.parseObject("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();`enter code here`
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:+ gmtDateFormat.format(dd));
There are a few problems in your code. For example, the format string doesn't match the actual format of the string you are parsing.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Object dd = null;
try {
dd = sdf.parse("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd));
To print the current date in whatever timezone you like, set the timezone you want to use on the SimpleDateFormat object. For example:
// Create a Date object set to the current date and time
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(now));
df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println("Current date and time in IST: " + df.format(now));

Categories