This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 9 years ago.
I have a date variable in the YYYY-MM-DD format.
How can I change the date value to the previous day? So if the value of the variable was 2014-01-01 it would change to 2014-12-31.
You can use a DateFormat and a Calendar, like so
String fmt = "yyyy-MM-dd";
String dt = "2014-01-01";
java.text.DateFormat df = new java.text.SimpleDateFormat(fmt);
java.util.Calendar cal = java.util.Calendar.getInstance();
try {
cal.setTime(df.parse(dt));
cal.add(java.util.Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());
} catch (Exception e) {
}
Which outputs
Tue Dec 31 00:00:00 EST 2013
Java can parse a date, then subtract one day and output the toString()
documentation: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html
Long version:
String example = "2014-01-01";
DateFormat df = new SimpleDateFormat("YYY-MM-dd", Locale.ENGLISH);
Date result = df.parse(target);
Calendar cal = Calendar.getInstance();
cal.setTime(result);
cal.add(Calendar.DATE, -1);
result = cal.getTime();
System.out.println(df.format(result));
Related
This question already has answers here:
Is SimpleDateFormat in Java work incorrect or I did any mistake? See code sample [duplicate]
(2 answers)
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Closed 3 years ago.
I want to add 30 days in my current date I searched a lot but did not get the proper solution.
my code
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
Calendar c1 = Calendar.getInstance();
String currentdate = df.format(date);
try {
c1.setTime(df.parse(currentdate));
c1.add(Calendar.DATE, 30);
df = new SimpleDateFormat("yyyy-MM-dd");
Date resultdate = new Date(c1.getTimeInMillis());
String dueudate = df.format(resultdate);
Toast.makeText(this, dueudate, Toast.LENGTH_SHORT).show();
} catch (ParseException e) {
e.printStackTrace();
}
The output of this code is :
2019-01-29
I don't why it is showing this output can anyone help me.
You need to use
c1.add(Calendar.DAY_OF_YEAR, 30);
instead of
c1.add(Calendar.DATE, 30);
Try this
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
Calendar c1 = Calendar.getInstance();
String currentDate = df.format(date);// get current date here
// now add 30 day in Calendar instance
c1.add(Calendar.DAY_OF_YEAR, 30);
df = new SimpleDateFormat("yyyy-MM-dd");
Date resultDate = c1.getTime();
String dueDate = df.format(resultDate);
// print the result
Utils.printLog("DATE_DATE :-> "+currentDate);
Utils.printLog("DUE_DATE :-> "+dueDate);
OUTPUT
2019-06-04 14:43:02.438 E/XXX_XXXX: DATE_DATE :-> 2019-06-04
2019-06-04 14:43:02.438 E/XXX_XXXX: DUE_DATE :-> 2019-07-04
Another easier option, if on Java 8, use the java.time package which provides functions to perform plus/minus on current date of any units of time, example:
import java.time.LocalDate;
LocalDate date = LocalDate.now().plusDays(30);
//or
LocalDate date = LocalDate.now().plus(30, ChronoUnit.DAYS);
Calendar.getInstance() gives you the current time. You don't need to create another Date object for that.
Calendar current = Calendar.getInstance();
current.add(Calendar.DATE, 30);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date resultdate = new Date(current.getTimeInMillis());
String dueudate = df.format(resultdate);
System.out.println("" + dueudate);
1)Go from date to millis.
2)Create a long variable with value 30L * 24L * 60L * 60L * 1000L.
3)Add this value to the millis you got in step 1
4) Go from this sum back to date again.
Edit: Variables that store millis should be long, not int.
Edit2: Adding "L" besides each number guarantees we won't get an overflow.
This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 5 years ago.
I have a problem in date conversion in my android application.
I have date strings like 2017-11-11 11:52 which its date is equal to 2017-Nov-11 but it is parsed as 2017-01-11 in below code snippet:
DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
try {
Date date = df.parse("2017-11-11 11:52");
Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
The log output of above code is "Wed Jan 11 11:52:00 GMT+03:30 2017".
Is there anything wrong in my date format string?
You are using a wrong dateformat.
DD stands for the day of the year, not the day of the month. You have to use dd instead.
You can check the SimpleDateFormat documentation, where it is stated that DD can range from 1 to 365.
So your code should look like this:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = df.parse("2017-11-11 11:52");
Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
D is day in year e.g 189. Use d instead
Clearly noted from your output : Read Document
D is Day in year (1-365)
d is day in month (1-31)
Change this
DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
to
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
D is used for (Day in year), so in you case you need to use d (is day in month ). please use this Format "yyyy-MM-dd HH:mm"
This question already has answers here:
Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java [duplicate]
(2 answers)
Closed 6 years ago.
I am having issues converting a String that has this format for date from the server into a long?
Example Date String - "2016-07-04T00:02:34.457Z" (Note this is a string)
I tried this below but needs try catch around gmt, when I add it and a not null around cmtDt - then I initialize cmtDt to 0 pre setting it on the bottom and it is always 0.
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
formatter.setTimeZone(tz);
Date gmt = formatter.parse(comment.getDateCommented());
cmtDt = gmt.getTime();
First, your input String includes milliseconds (and your format does not). Second, your input String includes a literal Z (which is presumably to indicate a UTC timezone). Finally, getting your system timezone and assigning it to the formatter isn't reliably going to be UTC. You need something like,
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println(cmtDt);
} catch (Exception e) {
e.printStackTrace();
}
Which I ran, and got
1467590554457
Your format string for the SimpleDateFormat needs to be:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
My test code that works is:
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
formatter.setTimeZone(tz);
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println("cmtDt = " + cmtDt);
The format in SDF needs to be fixed. The following will help you.
Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
formatter.setTimeZone(tz);
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println(cmtDt);
Prints : 1467599640457
This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29
The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".
SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour
This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 8 years ago.
Server sending me time as 1390361405210+0530 so if I want to convert this in to date then should I have to add 0530 into 1390361405210 and then calculate date and time?
Any suggestion should be appreciated.Thanks
How about this.
long currentDateTime = 1390361405210L;
Date currentDate = new Date(currentDateTime);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss Z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+530"));
System.out.println(sdf.format(currentDate));
public static void main( String[] args )
{
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
long milliSeconds=1390361405210L;
Date date = new Date(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));
System.out.println(formatter.format(date));
}
If we consider that the first part of the String is the number of milliseconds since the epoch, and the second part is a timezone indication (in that case, IST, Indian Standard Time), you can get a readable date like this :
final String jsonDate = "1390361405210+0530";
final Date date = new Date(Long.parseLong(jsonDate.substring(0, jsonDate.length() - 5)));
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT" + jsonDate.substring(jsonDate.length() - 5)));
System.out.println(format.format(date));
Output:
January 22, 2014 9:00:05 AM GMT+05:30