Sorry for the apology for asking repeated question..
public static void main(String[] args)throws Exception {
GregorianCalendar gcal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
gcal.setTime(start);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
}
In the above code prints between dates exactly but i need to print start date also..
above code output is
Mon Feb 01 00:00:00 IST 2010
Mon Mar 01 00:00:00 IST 2010
Thu Apr 01 00:00:00 IST 2010
But i need also start date on my output..
please help me to get this
Thanks in advance..
In my opinion this is the nicest way:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(start);
while (!gcal.getTime().after(end)) {
Date d = gcal.getTime();
System.out.println(d);
gcal.add(Calendar.MONTH, 1);
}
Output:
Fri Jan 01 00:00:00 WST 2010
Mon Feb 01 00:00:00 WST 2010
Mon Mar 01 00:00:00 WST 2010
Thu Apr 01 00:00:00 WST 2010
All we do is print the date before incrementing it, then we repeat if the date is not after the end date.
The other option is to duplicate the printing code before the while (yuck) or to use a do...while (also yuck).
You could use a do-while loop, but you would need to alter the end date depending on whether you want to include it or not.
The example below includes all months between 01st or 04th inclusive...
try {
Calendar gcal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.05");
gcal.setTime(start);
do {
Date d = gcal.getTime();
System.out.println(d);
gcal.add(Calendar.MONTH, 1);
} while (gcal.getTime().before(end));
} catch (ParseException exp) {
exp.printStackTrace();
}
Example output...
Fri Jan 01 00:00:00 EST 2010
Mon Feb 01 00:00:00 EST 2010
Mon Mar 01 00:00:00 EST 2010
Thu Apr 01 00:00:00 EST 2010
Equally, you could simply use your current code and change the start Date to one month earlier...
Date start = sdf.parse("2009.12");
//...
Updated
Another approach, based on the previous ideas...
Simply move the start date back a month before you start the loop...
Calendar gcal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
gcal.setTime(start);
// Move the month back by one before we start...
gcal.add(Calendar.MONTH, -1);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
This is reasonably simple and allows you to supply variable dates without needing to care to remember that you need to start one month earlier....
You can just output it right before the start of the loop:
System.out.println(gcal.getTime());
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
then just do this:
Date lNow = gcal.getTime();
System.out.println(lNow);
while (gcal.getTime().before(end)) {
gcal.add(Calendar.MONTH, 1);
Date d = gcal.getTime();
System.out.println(d);
}
Since Java 8, you can use the new java.time API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu.MM");
YearMonth date = YearMonth.parse("2010.01", formatter);
YearMonth end = YearMonth.parse("2010.05", formatter);
while (!date.isAfter(end)) {
System.out.println(date.format(formatter));
date = date.plusMonths(1);
}
Related
When I select date in SQL it is returned as:
Wed Jan 31 00:00:00 GMT+01:00 2018
But I need only the Date part, that is Jan 31 2018. How can I do this?
There may be a reason to use org.jdesktop.swingx.JXDatePicker but rather of using JXDatePicker, I will simply show GMT parsing using java.util.Date.
Try this source code:
System.out.println(new Date());
//will show your Date along with your local TimeZone
//result for me is : Sat Jan 13 08:47:59 IST 2018
//First changing local pacific time zone to GMT+01 level explicitly,
//otherwise it will show results as your local time zone by default.
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
String existingDateValue = "Wed Jan 31 00:00:00 GMT+01:00 2018";
DateFormat gmtFormat =
new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
try {
//try to parse and validate existing date
Date validatedExistingDate = gmtFormat.parse(existingDateValue);
System.out.println(validatedExistingDate);
//parsed validated date : Wed Jan 31 00:00:00 GMT+01:00 2018
DateFormat newFormat = new SimpleDateFormat("MMM d, yyyy");
System.out.println(newFormat.format(validatedExistingDate));
//required Date is in GMT format : Jan 31, 2018
} catch (ParseException pex) {
pex.printStackTrace();
} finally {
System.out.println("Current TimeZone : " + new Date());
//now, reverting to my local TimeZone
TimeZone.setDefault(TimeZone.getTimeZone("IST"));
System.out.println("Current TimeZone : " + new Date());
}
I have taken this date "2016-04-26 12:00:00”, and converted to GMT and CST epochs, using the function below. I got the dates below. Not sure I am doing anything wrong here.
1461672000000 UTC ——> Tue, 26 Apr 2016 12:00:00 GMT
1461690000000 CST —> Tue, 26 Apr 2016 17:00:00 GMT
Code:
long epoch = 0;
String str = "2016-04-26 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("CST")); //This GMT or CST
Date datenew = df.parse(str); //parsethe date
epoch = datenew.getTime(); //get the epoch time
As eluded to by Erickson in the comments, your expectations seem inverted from the implementation; when you set the TimeZone in the DateFormat, using the DateFormat.parse() method results in the string it's parsing as if it is coming from that TimeZone (and converts it to the local time). So the results you notice are exactly expected.
To fix this, use the DateFormat.format() method instead:
public static void main(String[] args) {
String dateStr = "2016-04-26 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date gmtDate = null;
try {
gmtDate = df.parse(dateStr);
}
catch (ParseException e) {
e.printStackTrace();
}
System.out.println("GMT TIME = " + df.format(gmtDate));
df.setTimeZone(TimeZone.getTimeZone("CST"));
System.out.println("CST TIME = " + df.format(gmtDate));
}
Output:
GMT TIME = 2016-04-26 12:00:00
CST TIME = 2016-04-26 07:00:00
http://ideone.com/T5wSRV this is the link to below code
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Time in IST
Date date=dateFormatIST.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
this is not giving correct IST time where as code below is working fine . why?
http://ideone.com/9KSaZx this is the link to below code which is giving the desired output.Help me understand the behavior.
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date date=dateFormatLocal.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
The behaviour is logical. The point is that there is no information of time-zone is a Date object. A Date object contains Universal Time.
And when you format then parse the formatted string, you still have the same date:
I commented the code with the results:
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date d = new Date();
System.out.println(d);
// Mon Mar 16 16:57:19 CET 2015
=> now in my TZ (CET)
System.out.println(dateFormatIST.format(d));
// 2015-Mar-16 21:27:19
=> now in IST TZ
System.out.println(dateFormatLocal.format(d));
// 2015-Mar-16 16:57:19
=> now in my TZ (CET)
Date dateIST = dateFormatIST.parse(dateFormatIST.format(d));
System.out.println(dateIST);
// Mon Mar 16 16:57:19 CET 2015
=> The dateIST object contains still "now", and the format is default local which is CET
Date dateLoc = dateFormatLocal.parse(dateFormatLocal.format(d));
System.out.println(dateLoc);
// Mon Mar 16 16:57:19 CET 2015
=> same thing as above
Date dateLocIST = dateFormatLocal.parse(dateFormatIST.format(d));
System.out.println(dateLocIST);
// Mon Mar 16 21:27:19 CET 2015
=> dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015".
If you need to translate dates between different time-zone, you certainly need to go for the Calendar class.
I have a string of day, date and time that is String myDateString = "Fri, 07 Jun 2013 09:30:00";.For date January 2, 2010 we use new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(mystring);. What can I use instead of MMMM d, yyyy in my situation
Now How can I extract day, year, month, day of the month, hours, minutes and seconds from the string in the following pattern.
day: Fri,
Year: 2013,
Month: Jun,
day of the Month: 07,
Hour: 09,
Minutes: 30 and
Seconds: 00,
Please help me in this respect I would be very thankful to you for this act of kindness. Thanks in advance.
Reference these formats Java Date Format Docs:
try this code:
Date tempDate = new SimpleDateFormat("E, dd MM yyyy HH:mm:ss").parse("Fri, 09 12 2013 09:30:00");
System.out.println("Current Date " +tempDate);
Use a SimpleDateFormat object to extract the date and put it into a util.Date object. From there extract the individual attributes you need.
try this:
String myDateString = "Fri, 07 Jun 2013 09:30:00";
Date myDate = null;
// attempting to parse the String with a known format
try {
myDate =
new SimpleDateFormat("E, dd MMM yy HH:mm:ss", Locale.ENGLISH)
.parse(myDateString);
}
// something went wrong...
catch (Throwable t) {
// just for debug
t.printStackTrace();
}
finally {
if (myDate != null) {
// just for checking...
System.out.println(myDate);
// TODO manipulate with calendar
}
}
This will work if you are certain that the format you receive will always be consistent.
You can then split your date into different values by initializing a Calendar object, then retrieving its various fields.
For instance:
// once you're sure the date has been parsed
Calendar calendar = Calendar.getInstance(myTimeZone, myLocale);
calendar.setTime(myDate);
// prints the year only
System.out.println(calendar.get(Calendar.YEAR));
You can use SimpleDateFormat from standard library. Something like following:
new SimpleDateFormat("E, dd MM yyyy HH:mm:ss").parse(myDateString);
The easiest solution is to split the string
String[] parts = "Fri, 07 Jun 2013 09:30:00".split("[ :,]+");
this will produce an array
[Fri, 07, Jun, 2013, 09, 30, 00]
then use its elements
String dayOfWeek = parts[0];
...
What is the date format to get only hours in 12-hours format from this time
Thu Oct 20 13:12:00 GMT+02:00 2011
edit:
using this code
Date eventDate = tempAppointments.get(i).mStartDate
System.out.println(eventDate.toString());
// date pattern
DateFormat df = new SimpleDateFormat("hh:'00' a");//output : Wed Nov 09 11:00:00 GMT+02:00 2011
// get the start date with new format (pattern)
String hours = df.format(tempAppointments.get(i).mStartDate.getDay());
System.out.print(hours);//output: 02:00 AM
return hours as
02:00 AM
but for the given time. it must be 02:00 PM . why ?
I'm not sure why you are passing date.getDay() (which is deprecated, by the way) into the formatter if you want the hour part.
Try this:-
Date date = new Date();
System.out.println("Date: " + date);
DateFormat df = new SimpleDateFormat("hh:'00' a");
String hour = df.format(date);
System.out.println("Hour: " + hour);
The output:
Date: Sat Nov 19 17:57:05 CST 2011
Hour: 05:00 PM
Date fecha = new Date();
System.out.println("Fecha "+fecha);
DateFormat formato = new SimpleDateFormat("hh:mm:ss");
String hora = formato.format(fecha);
System.out.println("Son las "+hora);