How can i get String date from calendar?
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MOUNTH, -5); //set now and 5 days to back
I want get String like this(date on interval -5 days to TODAY):
11.03.2015
10.03.2015
.
.
.
07.03.2015
It's possible? How?
you can use for loop and reduce one day from calendar instance and print it
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
for (int index = 1; index <= 5; index++) {
calendar.add(Calendar.DATE, -1);
System.out.println(dateFormat.format(calendar.getTime()));
}
output:
10.03.2015
09.03.2015
08.03.2015
07.03.2015
06.03.2015
You should use the SimpleDateFormat class, as follows.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MOUNTH -5);
SimpleDateFormat myDateFormat = new SimpleDateFormat("MM.dd.yyyy"); //or ("dd.MM.yyyy"), If you want days before months.
String formattedDate = myDateFormat.format(cal.getTime());
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
Long beforeTime = date - (5*24*60*60*1000);
Date beforeDate = new Date(beforeTime);
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
String s = format.format(beforeDate);
s returns the date in your required format.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String strdate = sdf.format(calendardate.getTime());
Related
I have a unix timestamp. I wanted to convert into hours,min and seconds.I wanted to acheive it in java.I tried this .But I am not sure how do i have to concatenate it to hours+min+sec
int day = (int)TimeUnit.SECONDS.toDays(timeStamp);
long hours = TimeUnit.SECONDS.toHours(timeStamp) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(timeStamp) - (TimeUnit.SECONDS.toHours(timeStamp)* 60);
long second = TimeUnit.SECONDS.toSeconds(timeStamp) - (TimeUnit.SECONDS.toMinutes(timeStamp) *60);
thanks,
Ramya.
You can use the Calendar class for this. You can format the time using SimpleDateFormat
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeStamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String dateString = sdf.format(calendar.getTime());
Date date = new Date ();
date.setTime((long)unix_time*1000);
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss a",Locale.ENGLISH);
System.out.println(df.format(date));
I want to increment the date by one. I have the below code while running the code I am getting unparseable date finally I want the date as string in the format of MM-DD-YYYY.
But same program is working with the YYYY-MM-DD format but i want mydate in this format(MM-DD-YYYY)
String dt = schReq.getStartDate(); // Start date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, days); // number of days to add
dt = sdf.format(c.getTime());
schReq.setStartDate(dt);
Can anyone please help me?
The code should be working fine as long as dt and days are correct. This gave me 12-18-2014:
String dt = "12-17-2014"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime());
You have to use two different DateFormats:
one for parsing the string and one for formatting.
String dt = schReq.getStartDate(); // Start date
SimpleDateFormat sdf_parser = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf_parser.parse(dt));
c.add(Calendar.DATE, days); // number of days to add
dt = sdf.format(c.getTime());
schReq.setStartDate(dt);
I want to compare two date objects like this:
String format = "MM/dd/yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
fakeDate = sdf.parse("15/07/2013 11:00 AM");
fakeDate2 = sdf.parse("15/07/2013 12:00 AM");
int diff = date2.getHours() - date1.getHours();
but then I see getHours is deprecated.
So i have used:
Calendar calendar1 = Calendar.getInstance();
calendar.setTime(new Date("15/7/2013 11:00AM"));
Calendar calendar2 = Calendar.getInstance();
calendar.setTime(new Date("11/7/2013 12:00AM"));
calendar1.get(Calendar.HOUR_OF_DAY) - calendar2.get(Calendar.HOUR_OF_DAY)
but then I see the diff is zero. I guess i change the same calendar instance all the time and compare it to itself. no?
how would you write this?
new Date("15/7/2013 11:00AM")
is not the correct way to construct a Date object. It's deprecated also. Use proper SimpleDateFormat as you are doing well in your first example.
The format MM/dd/yyyy hh:mm a is not correct as per the input date string.
It should be like this.
String format = "dd/MM/yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Date fakeDate2 = sdf.parse("15/07/2013 12:00 AM");
Calendar cal = Calendar.getInstance();
cal.setTime(fakeDate); //set time to first date
int hours1 = cal.get(Calendar.HOUR); // get the hours
cal.setTime(fakeDate2); // set time to second date
int hours2 = cal.get(Calendar.HOUR); // get the hours
System.out.println(hours1 - hours2); // 11 hours
You seemed really confused though it is depreciated this code shall give you a difference of 11 hours which makes sense
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date fakeDate = dateFormat.parse("15/07/2013 11:00 AM");
Date fakeDate2 = dateFormat.parse("15/07/2013 12:00 AM");
System.out.println(fakeDate.getHours()-fakeDate2.getHours());
Or if you are so interested to use Calendar do it this way
Calendar calendar = Calendar.getInstance();
calendar.setTime(fakeDate);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(fakeDate2);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY) -
calendar2.get(Calendar.HOUR_OF_DAY));
You will get the same 11 hours difference or simply do it this way
long millisForNewDate = toDate.getTime();
long millisForOldDate = fromDate.getTime();
long difference = millisForNewDate - millisForOldDate;
int hoursDifferenceBetweenDates = (int)(difference/(1000*60*60));
This question already has answers here:
How to check if a date Object equals yesterday?
(9 answers)
Closed 8 years ago.
The following function produces today's date; how can I make it produce only yesterday's date?
private String toDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date).toString();
}
This is the output:
2012-07-10
I only need yesterday's date like below. Is it possible to do this in my function?
2012-07-09
Update
There has been recent improvements in datetime API with JSR-310.
Instant now = Instant.now();
Instant yesterday = now.minus(1, ChronoUnit.DAYS);
System.out.println(now);
System.out.println(yesterday);
https://ideone.com/91M1eU
Outdated answer
You are subtracting the wrong number:
Use Calendar instead:
private Date yesterday() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
Then, modify your method to the following:
private String getYesterdayDateString() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return dateFormat.format(yesterday());
}
See
IDEOne Demo
You can do following:
private Date getMeYesterday(){
return new Date(System.currentTimeMillis()-24*60*60*1000);
}
Note: if you want further backward date multiply number of day with 24*60*60*1000 for example:
private Date getPreviousWeekDate(){
return new Date(System.currentTimeMillis()-7*24*60*60*1000);
}
Similarly, you can get future date by adding the value to System.currentTimeMillis(), for example:
private Date getMeTomorrow(){
return new Date(System.currentTimeMillis()+24*60*60*1000);
}
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
Use Calender Api
Try this one:
private String toDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// Create a calendar object with today date. Calendar is in java.util pakage.
Calendar calendar = Calendar.getInstance();
// Move calendar to yesterday
calendar.add(Calendar.DATE, -1);
// Get current date of calendar which point to the yesterday now
Date yesterday = calendar.getTime();
return dateFormat.format(yesterday).toString();
}
Try this;
public String toDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return dateFormat.format(cal.getTime());
}
changed from your code :
private String toDate(long timestamp) {
Date date = new Date (timestamp * 1000 - 24 * 60 * 60 * 1000);
return new SimpleDateFormat("yyyy-MM-dd").format(date).toString();
}
but you do better using calendar.
There is no direct function to get yesterday's date.
To get yesterday's date, you need to use Calendar by subtracting -1.
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
Sample Julian Dates:
2009218
2009225
2009243
How do I convert them into a regular date?
I tried converting them using online converter and I got-
12-13-7359 for 2009225!! Makes no sense!
Use the Joda-Time library and do something like this:
String dateStr = "2009218";
MutableDateTime mdt = new MutableDateTime();
mdt.setYear(Integer.parseInt(dateStr.subString(0,3)));
mdt.setDayOfYear(Integer.parseInt(dateStr.subString(4)));
Date parsedDate = mdt.toDate();
Using the Java API:
String dateStr = "2009218";
Calendar cal = new GregorianCalendar();
cal.set(Calendar.YEAR,Integer.parseInt(dateStr.subString(0,3)));
cal.set(Calendar.DAY_OF_YEAR,Integer.parseInt(dateStr.subString(4)));
Date parsedDate = cal.getTime();
---- EDIT ----
Thanks for Alex for providing the best answer:
Date myDate = new SimpleDateFormat("yyyyD").parse("2009218")
Another format is CYYDDDD I wrote this function in Java
public static int convertToJulian(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
String syear = String.format("%04d",year).substring(2);
int century = Integer.parseInt(String.valueOf(((year / 100)+1)).substring(1));
int julian = Integer.parseInt(String.format("%d%s%03d",century,syear,calendar.get(Calendar.DAY_OF_YEAR)));
return julian;
}