This question already has answers here:
Convert Date to String using simpledateformat
(5 answers)
Closed 6 years ago.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String dateInString = "2016-04-23";
try {
Date date = formatter.parse(dateInString);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println("Date: -- "+date);
System.out.println("FormatterDate : -- "+formatter.format(date));
System.out.println("SQLDATE : -- "+sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output :
Date: -- Sat Jan 23 00:04:00 CST 2016
FormatterDate : -- 2016-04-23
SQLDATE : -- 2016-01-23
My out put should be 2016-04-23 not 2016-01-23
As per the javadocs
m Minute in hour
You want
M Month in year
Kindly note that mm stands for Minute and not Month. For month you have to use MM instead.
Related
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.
This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
How to add one day to a date? [duplicate]
(18 answers)
When Using Date Picker In Android It is Picking the Wrong Date [duplicate]
(1 answer)
Closed 3 years ago.
I'm trying to add days that exceed the month days. example July 1,2019 and I add 32 days so the result would be August 2,2019.
SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyy");
SimpleDateFormat Dateformat = new SimpleDateFormat("mm/dd/yyy");
String getDate = date_pick.getText().toString();
Date mDate;
Date result_desu;
try {
mDate = format.parse(getDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(mDate);
calendar.add(Calendar.DATE, 32);
String formattedDate = Dateformat.format(calendar.getTime());
date_result.setText(formattedDate); // format output
} catch (ParseException e) {
e.printStackTrace();
}
I've been using this code but it turns out the days only reset with the same month example: July 1,2019 ; result: July 2,2019.
Try this:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Main{
public static void main(String args[]){
String oldDate = "2019-07-1";
System.out.println("Date before Addition: "+oldDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try{
c.setTime(sdf.parse(oldDate));
}catch(ParseException e){
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, 32);
String newDate = sdf.format(c.getTime());
System.out.println("Date after Addition: "+newDate);
}
}
Output:
Date before Addition: 2019-07-1
Date after Addition: 2019-08-02
This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 4 years ago.
I wish to add one day to a given date.If i pass 2018-08-05,the below method returns 2018-08-06 which is expected. But if the pass the last date of the month -2018-08-31,it returns 2018-08-01 instead of expected result 2018-09-01.
DateFormat format = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);
Date date = null;
try {
date = format.parse("2018-08-31");
} catch (ParseException e) {
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE ,1);
return format.format(cal.getTime());
You're using the old calendar/date API. This API is quite bad (it does weird things and does not accurately model how dates actually work).
It has been replaced with the new java.time API. I strongly suggest you use that instead. If you're on java7 or below, you can use the 'JSR310-backport' library to your dependency list to use this API. (JSR310 is the name for this addition to java).
In java.time, you'd do:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse("2018-08-31", fmt);
System.out.println(fmt.format(date.plusDays(1)));
// yyyy-MM-dd so happens to be the default for LocalDate, so...
// we can make it a lot simpler:
date = LocalDate.parse("2018-08-31");
System.out.println(date.plusDays(1));
}
}
The bug is in the pattern of your SimpleDateFormat which you use for input and output. This dual-use masks the error:
public class Main {
public static void main(String[] args) throws Exception {
DateFormat format = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);
Date date = format.parse("2018-08-31");
System.out.println("format: " + format.format(date) +", real: " + date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE ,1);
System.out.println("format: " + format.format(cal.getTime()) +", real: " + cal.getTime());
}
}
This gives you:
format: 2018-08-31, real: Wed Jan 31 00:08:00 CET 2018
format: 2018-08-01, real: Thu Feb 01 00:08:00 CET 2018
Using the right pattern yyyy-MM-dd produces the right answer:
format: 2018-08-31, real: Fri Aug 31 00:00:00 CEST 2018
format: 2018-09-01, real: Sat Sep 01 00:00:00 CEST 2018
Since the new and the old Java-Time API use the same patterns, simply adopting the new API will not help in this case.
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = null;
try {
date = format.parse("2018-08-31");
} catch (ParseException e) {
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.setTimeInMillis(cal.getTimeInMillis() + 86400000); //86400000ms = 1 day
return format.format(cal.getTime());
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
How to parse the following timestamp in string formate to date formate.
Mon Sep 25 13:40:56 GMT+05:30 2017
Since it has GMT in the timestamp so I didn't find a valid answer for this. Please let me know how to write SimpleDateFormat for this?
in java 7 with SimpleDateFormat
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String input = "Mon Sep 25 13:40:56 GMT+05:30 2017";
String dateFormat = "EEE MMM d HH:mm:ss z yyyy";
try {
Date date = new SimpleDateFormat(dateFormat).parse(input);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
See it in action : https://ideone.com/RaCugz
This question already has answers here:
Java Date year calculation is off by year for two days
(5 answers)
Closed 5 years ago.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class myTests {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("YYYY-MMM-dd", Locale.US);
try {
Date sortingDate = (Date)formatter.parse("2017-Jul-13");
System.out.println("Sorted Date is:"+sortingDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result is
Sorted Date is:Sun Jan 01 00:00:00 PST 2017
Why wont it show date i gave 2017 Jul 13
Can you please let me know.
Thanks
Abe
uppercase Y is Week year. What you Need is lowercase y = Year.
So Change new SimpleDateFormat("YYYY-MMM-dd", Locale.US); to new SimpleDateFormat("yyyy-MMM-dd", Locale.US); and you should get the correct result.
For more informations see the javadoc of SimpleDateFormat
If you are using java8, you should Change to DateTimeFormatter and the new DateTime API