How to get current date - 5 in JSP? [duplicate] - java

This question already has answers here:
How to get previous date in java
(11 answers)
Closed 7 years ago.
I am trying to get date which is 5 days from today, my code snippet looks like this -
Date curDate = new Date();
out.println(curDate);
String pattern = "yyyy.MM.dd";
DateFormat format1 = new SimpleDateFormat(pattern, Locale.ENGLISH);
//String DateToStr = format1.format(curDate);
String DateToStr = format1.format(curDate);
out.println(DateToStr);
Date date = format1.parse(DateToStr);
Date prevdate = DateUtils.addDays(Date(), -5);
Though getting the current date 2016.02.19, i am not able to nail 5 days before which is 2016.02.14.
Any suggestions what's that i am doing wrong here? Or any better way to do this?
Appreciate your help.
Arun

import java.util.*;
import java.text.*;
public class HelloWorld
{
public static void main(String[] args)
{
java.text. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, -5); // 5 days back date
String output = sdf.format(c.getTime());
System.out.println(output);
}
}

Related

Date formatter numbers to text [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 4 years ago.
I have the date format as "dd/MM/yyyy", i would like to convert this into MM DD, yyy an example:
10/10/1996 -> Oct 10, 1996
Also the other way around case:
Oct 10, 1996 --> 10/10/1996
I have been trying for 1+ hour but couldnt figure it out as i have never used a date formatter class, if anyone could help me it would be awesome, PL: java
Im assuming your'e using DateTime.
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
String strDate = sdfDate.format(now);
This code will format a DateTime to string as 2018-04-07.
You can play around with the format as you want. search for list of the format keys (such as MM, as I mentioned here).
//Use these imports
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Use this code to convert your date format from one to another:
String fromDateFormat = "dd/MM/yyyy";
String fromdate = 15/03/2018; //Take any date
String CheckFormat = "dd MMM yyyy";//take another format like dd/MMM/yyyy
String dateStringFrom;
Date DF = new Date();
try
{
//DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat FromDF = new SimpleDateFormat(fromDateFormat);
FromDF.setLenient(false); // this is important!
Date FromDate = FromDF.parse(fromdate);
dateStringFrom = new
SimpleDateFormat(CheckFormat).format(FromDate);
DateFormat FromDF1 = new SimpleDateFormat(CheckFormat);
DF=FromDF1.parse(dateStringFrom);
System.out.println(dateStringFrom);
}
catch(Exception ex)
{
System.out.println("Date error");
}
Note: Change your SimpleDateFormat accordingly.

Convert string to date with format in java [duplicate]

This question already has answers here:
Get Date type object in format in java
(6 answers)
Closed 6 years ago.
I want to convert String to Date using following format: "yyyy-MM-dd"
public class TestProgram {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -2);
Date toDelete = new Date(cal.getTimeInMillis());
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
String formatDate = s.format(toDelete);
System.out.println( "Date before 2 Days: " + formatDate);
}
}
Current datatype format is String, I want to convert this in Date datatype, how to do that with this format [yyyy-MM-dd].?
What you're looking for is:
//formatDate is String containing date
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = s.parse(formatDate);
You could do something like this just to get Date format:
String yourdate = "2015-04-08";
Date date = java.sql.Date.valueOf(yourdate);
Alternatively I recommend using Apache/Joda libraries for date manipulation.

convert milliseconds to date [duplicate]

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

increment now date by several days issue in java [duplicate]

This question already has answers here:
how to add days to java simple date format
(2 answers)
Closed 8 years ago.
I need to increment date by some days.
private Date now = new Date();
private Date result;
public void incrementDate(Integer days) {
result =
}
So if days equals 3 i need to increment my now date on 3 days and set it to result.
I know that java 8 has plusDays method in LocalDate class. Is there a way how to implement this in java 7.
Use Calendar
Calendar cal = Calendar.getInstance ();
cal.setTime (now);
cal.add (Calendar.DATE, days);
plus other fun stuff.
Use Calendar to do this:
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE,3);
result = cal.getTime()
I suggest you make the function static and pass in now. return Date and use a Calendar. Something like,
public static Date incrementDate(Date now, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
And then to test it
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
System.out.println(df.format(now));
System.out.println(df.format(incrementDate(now, 3)));
}
Output here (today) is
2014-11-12
2014-11-15
try this code :
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar cal=Calendar.getInstance();
String today=sdf.format(cal.getTime());
System.out.println(today);
cal.add(Calendar.DATE, 20);
String After=sdf.format(cal.getTime());
System.out.println(After);
Date now = new Date();

Format current date to show day of the week [duplicate]

This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 6 years ago.
I want to show the current date in my application like this:
Thu, May 2, 2013
I already have the following code to get the current date
Calendar c = Calendar.getInstance();
Time time = new Time();
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH),
c.get(Calendar.YEAR));
How can I format this Time object to the string I need?
This does what you want
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);
Where strDate can be displayed in your textView or whatever
Maybe you can use it.
This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.
import java.text.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Date dt = new Date(1000000000000L);
DateFormat[] dtformat = new DateFormat[6];
dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
for(DateFormat dateform : dtformat)
System.out.println(dateform.format(dt));
}
}
output:
9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01
Source
Use this
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd,yyyy");
String formattedDate = formatter.format(new Date(System.currentTimeMillis()));
Log.e("formattedDate",formattedDate);
I will suggest to use java.text.SimpleDateFormat instead.
public static void main(String[] args) {
Date date=new Date();
String format = new SimpleDateFormat("EEE,MMM d,yyyy ").format(date);
System.out.println(format);
}
SimpleDateFormat dateformat= new SimpleDateFormat("dd,MM,yyyy");
String strdate = dateformat.format(new Date(System.currentTimeMillis()));
Oops, I'm a bit slow.

Categories