I would like to take a date object like "Sat Feb 17 20:49:54 +0000 2007" and change the year variable to the current year dynamically to something like this "Sat Feb 17 20:49:54 +0000 2012" what would be the best way to do this in java?
Construct a Calendar from the Date, use the Calendar to set the year, and then get back a Date object from the Calendar.
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.YEAR, 2012);
date = c.getTime();
If that's already a date object, you can do this:
Calendar cal = Calendar.getInstance();
int currentYear = cal.get(Calendar.YEAR);
cal.setTime(dateObj);
//set the year to current year
cal.set(Calendar.YEAR, currentYear);
//new date object with current year
dateObj = cal.getTime();
If that's a string, you can parse the string to a Java Date object first using SimpleDateFormat:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
And the use the above calendar object.
Based on what you asked for, this is how you do that:
try {
DateFormat dateFormat = new SimpleDateFormat("E, dd MMM HH:mm:ss Z yyyy");
date = (Date) dateFormat.parse("Sat, Feb 17 20:49:54 +0000 2007");
Calendar cal = dateFormat.getCalendar();
cal.set(Calendar.YEAR, 2012);
} catch (ParseException pe) {
//ParseException Handling
} catch(Exception e) {
//Exception Handling
}
Another option would be to utilize JodaTime API
import org.joda.time.DateTime;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class App
{
public static void main( String[] args )
{
//Sat Feb 17 20:49:54 +0000 2007
DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd H:m:s Z yyyy");
DateTime dt = fmt.parseDateTime("Sat Feb 17 20:49:54 +0000 2007");
MutableDateTime mdt = dt.toMutableDateTime();
mdt.setYear(new DateTime().getYear());
System.out.println(fmt.print(mdt));
}
}
Related
I am receiving date from the RSS Feed in the below format
Fri Oct 23 11:07:08 IST 2015 which i am trying to convert it into
yyyy-MM-dd HH:mm format .
I have tried this way
public class ConvertDate {
public static void main(String args[]) throws ParseException
{
String passedate = "Fri Oct 23 11:07:08 IST 2015";
String res= convertdate(passedate);
System.out.println(res);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate;
}
}
But i am getting
Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Oct 23 11:07:08 IST 2015"
at java.text.DateFormat.parse(Unknown Source)
at ConvertDate.convertdate(ConvertDate.java:20)
at ConvertDate.main(ConvertDate.java:12)
Could you please let em know how to resolve this
The date pattern does not match the input. Try change the line
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
to
SimpleDateFormat in = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Hope that helps
Your date has the format EEE MMM dd HH:mm:ss zzz yyyy with an english local.
This parses the date correctly:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)
.parse("Fri Oct 23 11:07:08 IST 2015");
Try this:
SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
in.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); //or Asia/Jerusalem
String s2 = "Fri Oct 23 11:07:08 IST 2015";
Date date = in.parse(s2);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
out.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(out.format(date));
Output:
2015-10-23 11:07
Also, note the setTimeZone. IST can either stand for Indian ST or Israel ST so it would be better if you specify which time zone you really want.
Check here for IST ambiguity.
First check your actual date which you need to work .. In my case
String day="date:10/01/2018";(In selenium need to get it from web page so i got the above string from page)
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);
When am working on this i got unparsable error....
So the day string contains some part of characters . So just remove those characters from string by using day.split(":"); String day1=day[1];
just give this day1 string in parse(); Now the updated code like as below..
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day1);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);
I want to convert the system date to yyyy-MM-dd format. There are similar questions in SO. I found that I need to parse the date in input format and then convert to the output format. But I am stuck at the first stage itself. I am not able to parse the system date as such (Sat Apr 25 14:44:15 IST 2015).
Here is my MWE:
import java.util.*;
import java.text.*;
public class Test
{
public static void main(String[] args)
{
try
{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY");
date = dateFormat.parse(date.toString());
System.out.println(date);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I get the exception as :
Unparseable date: "Sat Apr 25 14:53:33 IST 2015"
Date object can be converted to string of any date format.
String can be converted to date but it will come only in standard date format's but cant be in the one as you want..
If you want to format system date to yyyy-MM-dd format then use:
Date date = new Date();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
String date1 = dateFormater.format(date);
As you specified in comment you want to subtract sql date with current date then just convert the sql date to normal date format.
Like this:
String date = your date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(date);
Date currentdate = new Date();
Then use calender objects:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
Calendar calendar2 = calendar.getInstance();
calendar2.setTime(currentdate);
long difference = (calendar2.getTimeInMillis() - calendar
.getTimeInMillis()) / 60000;
This will give you the difference between two dates in minutes.
This will work for you
public class Test1 {
public static void main(String[] args) {
try
{
Date date = new Date();
System.out.println(date);
String dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY").format(date);
System.out.println(dateFormat);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Output
Sat Apr 25 15:10:38 IST 2015
Sat 04 25 15:10:38 PM 2015
I think you should do it like that.
Date date = new Date();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
date = format.parse(formattedDate);
System.out.println(date);
But you should understand difference between "date" and "date format".
I am trying to find out the specific date from a given input string, which can be like "201411W3". I know that the week is 3rd from this string(W3) and the event will be on Friday, so I want to find the date of the 3rd Friday. I did something like this:
public static Date getLastFriday( int month, int year ) {
Calendar cal = Calendar.getInstance();
cal.set( year, month, 1 );
cal.add( Calendar.DAY_OF_MONTH, - ( cal.get( Calendar.DAY_OF_WEEK ) % 7 + 8 ) );
return cal.getTime();
}
when I call this method: getLastFriday(11, 2014), I get the value "Fri Nov 21 13:16:57 EST 2014" which I need to parse to find out the date. is there any way to get just the date from the result?
Thanks!
If I understood you, then you can use below code as reference -
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test{
public static void main (String[] args)
{
String str="201411W3";
String[] strSplitted = str.split("W");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Integer.parseInt(strSplitted[0].substring(4,6))-1);
calendar.set(Calendar.YEAR, Integer.parseInt(strSplitted[0].substring(0,4)));
calendar.set(Calendar.DAY_OF_MONTH, 1);
if(calendar.get(Calendar.DAY_OF_WEEK)==7)
{
calendar.set(Calendar.WEEK_OF_MONTH, Integer.parseInt(strSplitted[1])+1);
}
else
{
calendar.set(Calendar.WEEK_OF_MONTH, Integer.parseInt(strSplitted[1]));
}
calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
System.out.println(formattedDate);
}
}
Output : 2014-11-21 You can change the format to any format you want.
If you just want to get the month and day without the seconds, you could call .get(Calendar.MONTH) and .get(Calendar.DATE) and pass them into the constructor of a new date object and return that object.
More info: here
Use this SimpleDateFormat
I didn't test the following code but it will work like:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date myDate = sdf.parse("Fri Nov 21 13:16:57 EST 2014");
Is it possible to remove the day (Fri), the time (22:34:21) and the time zone (GMT) by just having an output like "Jan 11 1980" instead of "Fri Jan 11 22:34:21 GMT 1980"??
Code below:
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 1980);
date.set(Calendar.MONTH, 0);
date.set(Calendar.DAY_OF_MONTH, 11);
Date dob = date.getTime();
System.out.println(dob);//Fri Jan 11 22:34:21 GMT 1980
Many thanks!
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(date));
Output:
Feb 26 2013
If you want a specific date, do
Calendar c = Calendar.getInstance();
c.set(1980, 0, 11);
Date date = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(date));
Prints
Jan 11 1980
public class DateFormat {
public static void main(String[] args) {
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 1980);
date.set(Calendar.MONTH, 0);
date.set(Calendar.DAY_OF_MONTH, 11);
Date dob = date.getTime();
System.out.println(new SimpleDateFormat("MMM dd yyyy").format(dob));
}
}
Output:
Jan 11 1980
Date is a representation of the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT)
In order to "remove" the time portion of a Date, you will want to use a DateFormat
Something as simple as;
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(dob));
Should work.
For a more localised version, you should use DateFormat.getDateInstance()
System.out.println(DateFormat.getDateInstance().format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM).format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(dob));
DateFormat dateFormatter = DateFormat.getDateInstance();
System.out.println(dateFormatter.format(date);
This will print the only the date corresponding to your current system locale settings.
See also: DateFormat in the JavaDoc
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(dob));
you can use:
stringToPrint = time.getMonth()+" "+time.getDate()+" "+time.getYear();
for more info:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html
I create a calendar object from a string date I receive.
Calendar cal = Calendar.getInstance();
String date = "example Mon, 22 Oct 2012 23:58:31 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
sdf.setTimeZone (TimeZone.getTimeZone ("PST"));
try {
cal.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("date "+sdf.format(cal.getTime()));
This date converts and prints the gmt time to pst with no problem.
I then loop through an array of the elements object and add seconds
int offset = element.getSeconds;
cal.add(Calendar.SECOND, offset);
System.out.println("times cal offset: " + cal.getTime());
This cal now prints the server time (which is eastern time) and not the converted gmt that is printed above. Does something bypass the cal created from the string gmt date when seconds are added?
Thanks!
This because you don't use SDF with setted GMT timezone for printing in second time.