Parse and retrieve timezone offset from date-time - java

Date format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Input date: "2017-09-18T03:08:20.888+0200"
Problem: I need retrieve timezone offset from the input String and print the parsed date in this timezone. In other words, I need output to be the same as the input.
SimpleDateFormat parses input date successfully and returns java.util.Date object. As we know, Date does not have timezone field. SimpleDateFormat converts parsed date to its timezone, which is, by default, system timezone. When I print this date, it is printed in System timezone.
Simple demo
private static void runDemoTask() throws ParseException {
final String dateTimeTimezoneFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
final SimpleDateFormat inputSdf = new SimpleDateFormat(dateTimeTimezoneFormat);
final String inputDate = "2017-09-18T01:08:20.888+0200";
Date parsedDate = inputSdf.parse(inputDate);
final SimpleDateFormat outputSdf = new SimpleDateFormat(dateTimeTimezoneFormat);
//outputSdf.setTimeZone("X_TIMEZONE_WHICH_I_NEED");
String output = outputSdf.format(parsedDate);
System.out.println(output);
}
Output
Mon Sep 18 00:08:20 GMT+01:00 2017
Note, output date has system timezone, which is different from input string.
Note, I will not use java.time, Joda Time and other libraries because I need to support existing code.
Possible unpleasant solution
I tried to use regular expression to retrieve sign and offset.
private static String parseTimeZone(String input) {
final int singGroup = 1;
final int offsetGroup = 2;
final String timezonePatternStr = "([+-])(\\d{4})$";
final Pattern timezonePattern = Pattern.compile(timezonePatternStr);
Matcher matcher = timezonePattern.matcher(input);
if (matcher.find()) {
String sign = matcher.group(singGroup);
String offset = matcher.group(offsetGroup);
System.out.println(sign + " " + offset);
}
return "";
}
It prints
+ 0200

Thank you, guys: #Thomas, #ole-v-v
final DateTimeFormatter inputSdf1 = DateTimeFormatter.ofPattern(dateTimeTimezoneFormat);
OffsetDateTime d = OffsetDateTime.parse(inputDate, inputSdf1);
ZoneOffset zo = d.getOffset(); //Offset from the input.
TimeZone tz = TimeZone.getTimeZone(zo.normalized());
outputSdf.setTimeZone(tz);
System.out.println(outputSdf.format(parsedDate));

tl;dr
TimeZone.getTimeZone( // Convert from modern java.time type (`ZoneOffset`/`ZoneId`) to legacy type (`TimeZone`)
OffsetDateTime.parse ( // Instantiate a `OffsetDateTime`, a moment on the timeline.
"2017-09-18T03:08:20.888+0200" ,
DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" )
).getOffset() // Extract a `ZoneOffset`, which is a subclass of `ZoneId`.
)
Convert directly from modern ZoneOffset to legacy TimeZone
The code seen here is similar to Answers by Yan Khonski, but using the variation of TimeZone.getTimeZone that directly converts from the modern java.time classes (ZoneOffset & ZoneID) to the legacy TimeZone class.
While there is no difference in the end result, this approach uses a an explicit conversion method. This is one of many new methods added to the old date-time classes for converting to/from java.time objects.
Using such a conversion method makes your code more self-documenting. Also makes more clear your awareness that you are consciously moving between the modern & legacy classes.
String input = "2017-09-18T03:08:20.888+0200";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse( input , f ); // Parse input.
ZoneOffset offset = odt.getOffset( ); // Interrogate for the `ZoneOffset` object representing this moment’s offset-from-UTC (a number of hours/minutes/seconds).
TimeZone tz = TimeZone.getTimeZone( offset ); // Convert from modern java.time object (a `ZoneOffset`/`ZoneId`) to the legacy class `TimeZone`.
Dump to console.
System.out.println( "odt.toString(): " + odt );
System.out.println( "offset.toString(): " + offset );
System.out.println( "tz.toString(): " + tz );
odt.toString(): 2017-09-18T03:08:20.888+02:00
offset.toString(): +02:00
tz.toString(): sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

SimpleDateFormat extends DateFormat and thus internally uses a Calendar. When parsing the date that calendar is being updated so you can get the timezone from it after parsing:
//use the timezone of the internally stored calendar
outputSdf.setTimeZone( inputSdf.getTimezone() );
That also shows why DateFormat is not threadsafe.
EDIT:
It seems the internal calendar's timezone isn't updated but the ZONE_OFFSET field is. Hence you could do something like this:
int zoneOffset = inputSdf.getCalendar().get( Calendar.ZONE_OFFSET );
//length check etc. left for you
String matchingZoneId = TimeZone.getAvailableIDs( zoneOffset )[0];
outputSdf.setTimeZone( TimeZone.getTimeZone( matchingZoneId ) );
Note that you can't just set the zone offset of the output format since that won't update the timezone reference which is used when formatting.
As you can see doing it this way looks a little "hacky" and thus you should think hard on whether you really need the timezone. In most cases you'd define the output timezone in a different way anyways, e.g. by getting the user's location, input, etc.

Hardcore solution!
Parse timezone and retrieve hours and minutes and sign and build a timezone!
public class ParseTimeZone {
private static final String TIME_ZONE_REGEX = "([+-])(\\d{2})[:]?(\\d{2})$";
private static final Pattern TIME_ZONE_PATTERN = Pattern.compile(TIME_ZONE_REGEX);
private static final int SING_GROUP = 1;
private static final int HOURS_GROUP = 2;
private static final int MINUTES_GROUP = 3;
private static final String PLUS = "+";
private static final String MINUS = "-";
private static final int MINUTES_IN_HOUR = 60;
private static final int SECONDS_IN_MINUTE = 60;
public static void main(String[] args) {
final String inputDate = "2017-09-18T01:08:20.888Z";
parseTimeZone(inputDate);
}
private static String parseTimeZone(String input) {
input = fixDateStringWithZeroZoneOffset(input);
Matcher matcher = TIME_ZONE_PATTERN.matcher(input);
if (!matcher.find()) {
return "";
}
String sign = matcher.group(SING_GROUP);
String hours = matcher.group(HOURS_GROUP);
String minutes = matcher.group(MINUTES_GROUP);
int offsetSeconds = calculateOffsetSeconds(sign, hours, minutes);
ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds);
System.out.println(zoneOffset);
TimeZone timeZone = TimeZone.getTimeZone(zoneOffset);
System.out.println(timeZone);
return "";
}
private static int calculateOffsetSeconds(String signStr, String hoursStr, String minutesStr) {
try {
int hours = Integer.parseInt(hoursStr);
int minutes = Integer.parseInt(minutesStr);
int sign = parseSign(signStr);
int seconds = sign * ((hours * MINUTES_IN_HOUR + minutes) * SECONDS_IN_MINUTE);
return seconds;
} catch (NumberFormatException e) {
throw new RuntimeException("It should not happen because it matches the regular expression. There should be numbers.", e);
}
}
private static int parseSign(String sign) {
if (sign.equals(PLUS)) {
return 1;
} else if (sign.equals(MINUS)) {
return -1;
} else {
throw new RuntimeException("Offset sign should be + or -.");
}
}
private static String fixDateStringWithZeroZoneOffset(String dateString) {
if (dateString.endsWith("Z")) {
return dateString.replaceAll("Z$", "+0000");
}
return dateString;
}
}

Related

Unable to compare two dates in android

I want to compare two dates to categories Browser History...
I have seen too many posts but didn't get any helpful,
My code is as :
private static String calculateDate()
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -10);
return simpleDateFormat.format(new Date(calendar.getTimeInMillis()));
}
private static String today()
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR,0);
return simpleDateFormat.format(new Date(calendar.getTimeInMillis()));
}
public void getBHistory()
{
long startdates = 0;
long enddates = 0;
Date endDate = null;
Date startDate=null;
try
{
startDate = (Date)new SimpleDateFormat("yyyy-MM-dd")
.parse(calculateDate());
endDate = (Date)new SimpleDateFormat("yyyy-MM-dd")
.parse(today());
startdates = startDate.getTime();
enddates = endDate.getTime();
} catch (ParseException e)
{
e.printStackTrace();
}
// 0 = history, 1 = bookmark
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0" + " AND "
+ Browser.BookmarkColumns.DATE + " BETWEEN ? AND ?";
Cursor mCur = m_oContext.getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, sel,
new String[]{
"" + startdates, "" + enddates
}, null);
mCur.moveToFirst();
String title = "";
String date_time = "";
if (mCur.moveToFirst() && mCur.getCount() > 0)
{
while (!mCur.isAfterLast())
{
title = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.TITLE));
date_time = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.DATE));
SimpleDateFormat simpleDate= new SimpleDateFormat("yyyy-MM-dd");
String curDate=simpleDate.format(new Date(Long.parseLong(date_time)));
Toast.makeText(m_oContext,"History Time : "+curDate,Toast.LENGTH_SHORT).show();
Toast.makeText(m_oContext,"Limit Time : "+calculateDate(),Toast.LENGTH_SHORT).show();
//TODO: Compare these two dates here
mCur.moveToNext();
}
}
}
I want to do if the History date is earlier than ten days ago then notify the user.
Any kind of help will be appreciated ,thank you.
tl;dr
Boolean alertUser =
LocalDate.parse( "2016-01-02" )
.isBefore(
LocalDate.now( ZoneId.of( “America/Montreal” ) )
.minusDays( 10 )
) ;
java.time
You are using troublesome old date-time classes now supplanted by the java.time classes.
Time zone
Your code ignores the crucial issue of time zone in determining a date such as “today”.
Example code
The LocalDate class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );
Your input strings are in standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.
LocalDate target = LocalDate.parse( "2016-01-02" );
You say the boundary is ten days ago. Use the plus or minus methods to determine future/past dates.
LocalDate tenDaysAgo = today.minusDays( 10 );
Compare using compareTo, equals, isBefore, and isAfter methods.
Boolean alertUser = target.isBefore( tenDaysAgo );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
Calendar is comparable so you can just use compare to. I would make curDate a calendar. Then (curDate.compareTo(calculatedDate) < 0) will be true if curDate is earlier than calculatedDate which you've set to ten days before today.
You can use
before()
Or
after()
to compare your calculated date with today's date
public boolean isHDateEarlier(String historyDate){
String[] historySplitStrings= historyDate.split("-");
String[] tenDaysEarlierStrings = calculateDate().split("-");
int historyYear = Integer.parseInt(historySplitStrings[0]);
int daysYear = Integer.parseInt(tenDaysEarlierStrings [0]);
int historyMonth = Integer.parseInt(historySplitStrings[1]);
int daysMonth = Integer.parseInt(tenDaysEarlierStrings [1]);
int historyDay = Integer.parseInt(historySplitStrings[2]);
int daysDay = Integer.parseInt(tenDaysEarlierStrings [2]);
if(historyYear < daysYear ){//check year
return true;
}
if(historyMonth < daysMonth &&
historyYear <= daysYear ){//check month
return true;
}
if(historyDay < daysDay &&
historyYear <= daysYear &&
historyMonth <= daysMonth){//check day
return true;
}
return false;
}
Just call :
isHDateEarlier(curDate);
I had a problem with comparing dates a week ago, and searched for answers and this helped me: Find nearest date from a list. - The last answer talks about NavigableSet<>
Try using NavigableSet<Date> such as TreeSet<> and put your dates in the list.
Than compare with lower or higher

Format ISO 8601 offset in Java [duplicate]

I have a date in the following format: 2010-03-01T00:00:00-08:00
I have thrown the following SimpleDateFormats at it to parse it:
private static final SimpleDateFormat[] FORMATS = {
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"), //ISO8601 long RFC822 zone
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"), //ISO8601 long long form zone
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), //ignore timezone
new SimpleDateFormat("yyyyMMddHHmmssZ"), //ISO8601 short
new SimpleDateFormat("yyyyMMddHHmm"),
new SimpleDateFormat("yyyyMMdd"), //birthdate from NIST IHE C32 sample
new SimpleDateFormat("yyyyMM"),
new SimpleDateFormat("yyyy") //just the year
};
I have a convenience method that uses those formats like so:
public static Date figureOutTheDamnDate(String wtf) {
if (wtf == null) {
return null;
}
Date retval = null;
for (SimpleDateFormat sdf : FORMATS) {
try {
sdf.setLenient(false)
retval = sdf.parse(wtf);
System.out.println("Date:" + wtf + " hit on pattern:" + sdf.toPattern());
break;
} catch (ParseException ex) {
retval = null;
continue;
}
}
return retval;
}
It seems to hit on the pattern yyyyMMddHHmm but returns the date as Thu Dec 03 00:01:00 PST 2009.
What is the correct pattern to parse this date?
UPDATE: I don't NEED the time zone parsing. I don't anticipate having time sensitive issues moving between zones, but how would I get the "-08:00" zone format to parse????
Unit test:
#Test
public void test_date_parser() {
System.out.println("\ntest_date_parser");
//month is zero based, are you effing kidding me
Calendar d = new GregorianCalendar(2000, 3, 6, 13, 00, 00);
assertEquals(d.getTime(), MyClass.figureOutTheDamnDate("200004061300"));
assertEquals(new GregorianCalendar(1950, 0, 1).getTime(), MyClass.figureOutTheDamnDate("1950"));
assertEquals(new GregorianCalendar(1997, 0, 1).getTime(), MyClass.figureOutTheDamnDate("199701"));
assertEquals(new GregorianCalendar(2010, 1, 25, 15, 19, 44).getTime(), MyClass.figureOutTheDamnDate("20100225151944-0800"));
//my machine happens to be in GMT-0800
assertEquals(new GregorianCalendar(2010, 1, 15, 13, 15, 00).getTime(),MyClass.figureOutTheDamnDate("2010-02-15T13:15:00-05:00"));
assertEquals(new GregorianCalendar(2010, 1, 15, 18, 15, 00).getTime(), MyClass.figureOutTheDamnDate("2010-02-15T18:15:00-05:00"));
assertEquals(new GregorianCalendar(2010, 2, 1).getTime(), MyClass.figureOutTheDamnDate("2010-03-01T00:00:00-08:00"));
assertEquals(new GregorianCalendar(2010, 2, 1, 17, 0, 0).getTime(), MyClass.figureOutTheDamnDate("2010-03-01T17:00:00-05:00"));
}
Output from unit test:
test_date_parser
Date:200004061300 hit on pattern:yyyyMMddHHmm
Date:1950 hit on pattern:yyyy
Date:199701 hit on pattern:yyyyMM
Date:20100225151944-0800 hit on pattern:yyyyMMddHHmmssZ
Date:2010-02-15T13:15:00-05:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
Date:2010-02-15T18:15:00-05:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
Date:2010-03-01T00:00:00-08:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
Date:2010-03-01T17:00:00-05:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
JodaTime's DateTimeFormat to rescue:
String dateString = "2010-03-01T00:00:00-08:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
System.out.println(dateTime); // 2010-03-01T04:00:00.000-04:00
(time and timezone difference in toString() is just because I'm at GMT-4 and didn't set locale explicitly)
If you want to end up with java.util.Date just use DateTime#toDate():
Date date = dateTime.toDate();
Wait for JDK7 (JSR-310) JSR-310, the referrence implementation is called ThreeTen (hopefully it will make it into Java 8) if you want a better formatter in the standard Java SE API. The current SimpleDateFormat indeed doesn't eat the colon in the timezone notation.
Update: as per the update, you apparently don't need the timezone. This should work with SimpleDateFormat. Just omit it (the Z) in the pattern.
String dateString = "2010-03-01T00:00:00-08:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateString);
System.out.println(date); // Mon Mar 01 00:00:00 BOT 2010
(which is correct as per my timezone)
if you used the java 7, you could have used the following Date Time Pattern. Seems like this pattern is not supported in the Earlier version of java.
String dateTimeString = "2010-03-01T00:00:00-08:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date date = df.parse(dateTimeString);
For More information refer to the SimpleDateFormat documentation.
Here's a snippet I used - with plain SimpleDateFormat. Hope somebody else may benefit from it:
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") {
public StringBuffer format(Date date, StringBuffer toAppendTo, java.text.FieldPosition pos) {
StringBuffer toFix = super.format(date, toAppendTo, pos);
return toFix.insert(toFix.length()-2, ':');
};
};
// Usage:
System.out.println(dateFormat.format(new Date()));
}
Output:
- Usual Output.........: 2013-06-14T10:54:07-0200
- This snippet's Output: 2013-06-14T10:54:07-02:00
Or... better, use a simpler, different, pattern:
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
// Usage:
System.out.println(dateFormat2.format(new Date()));
Output:
- This pattern's output: 2013-06-14T10:54:07-02:00
See the docs for that.
Try this, its work for me:
Date date = javax.xml.bind.DatatypeConverter.parseDateTime("2013-06-01T12:45:01+04:00").getTime();
In Java 8:
OffsetDateTime dt = OffsetDateTime.parse("2010-03-01T00:00:00-08:00");
If you can use JDK 1.7 or higher, try this:
public class DateUtil {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
public static String format(Date date) {
return dateFormat.format(date);
}
public static Date parse(String dateString) throws AquariusException {
try {
return dateFormat.parse(dateString);
} catch (ParseException e) {
throw new AquariusException(e);
}
}
}
document: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
which supports a new Time Zone format "XXX" (e.g. -3:00)
While JDK 1.6 only support other formats for Time Zone, which are "z" (e.g. NZST), "zzzz" (e.g. New Zealand Standard Time), "Z" (e.g. +1200), etc.
tl;dr
OffsetDateTime.parse( "2010-03-01T00:00:00-08:00" )
Details
The answer by BalusC is correct, but now outdated as of Java 8.
java.time
The java.time framework is the successor to both Joda-Time library and the old troublesome date-time classes bundled with the earliest versions of Java (java.util.Date/.Calendar & java.text.SimpleDateFormat).
ISO 8601
Your input data string happens to comply with the ISO 8601 standard.
The java.time classes use ISO 8601 formats by default when parsing/generating textual representations of date-time values. So no need to define a formatting pattern.
OffsetDateTime
The OffsetDateTime class represents a moment on the time line adjusted to some particular offset-from-UTC. In your input, the offset is 8 hours behind UTC, commonly used on much of the west coast of North America.
OffsetDateTime odt = OffsetDateTime.parse( "2010-03-01T00:00:00-08:00" );
You seem to want the date-only, in which case use the LocalDate class. But keep in mind you are discarding data, (a) time-of-day, and (b) the time zone. Really, a date has no meaning without the context of a time zone. For any given moment the date varies around the world. For example, just after midnight in Paris is still “yesterday” in Montréal. So while I suggest sticking with date-time values, you can easily convert to a LocalDate if you insist.
LocalDate localDate = odt.toLocalDate();
Time Zone
If you know the intended time zone, apply it. A time zone is an offset plus the rules to use for handling anomalies such as Daylight Saving Time (DST). Applying a ZoneId gets us a ZonedDateTime object.
ZoneId zoneId = ZoneId.of( "America/Los_Angeles" );
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );
Generating strings
To generate a string in ISO 8601 format, call toString.
String output = odt.toString();
If you need strings in other formats, search Stack Overflow for use of the java.util.format package.
Converting to java.util.Date
Best to avoid java.util.Date, but if you must, you can convert. Call the new methods added to the old classes such as java.util.Date.from where you pass an Instant. An Instant is a moment on the timeline in UTC. We can extract an Instant from our OffsetDateTime.
java.util.Date utilDate = java.util.Date( odt.toInstant() );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
Thanks acdcjunior for your solution. Here's a little optimized version for formatting and parsing :
public static final SimpleDateFormat XML_SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.FRANCE)
{
private static final long serialVersionUID = -8275126788734707527L;
public StringBuffer format(Date date, StringBuffer toAppendTo, java.text.FieldPosition pos)
{
final StringBuffer buf = super.format(date, toAppendTo, pos);
buf.insert(buf.length() - 2, ':');
return buf;
};
public Date parse(String source) throws java.text.ParseException {
final int split = source.length() - 2;
return super.parse(source.substring(0, split - 1) + source.substring(split)); // replace ":" du TimeZone
};
};
You can use X in Java 7.
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
static final SimpleDateFormat DATE_TIME_FORMAT =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static final SimpleDateFormat JSON_DATE_TIME_FORMAT =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
private String stringDate = "2016-12-01 22:05:30";
private String requiredDate = "2016-12-01T22:05:30+03:00";
#Test
public void parseDateToBinBankFormat() throws ParseException {
Date date = DATE_TIME_FORMAT.parse(stringDate);
String jsonDate = JSON_DATE_TIME_FORMAT.format(date);
System.out.println(jsonDate);
Assert.assertEquals(jsonDate, requiredDate);
}
Try setLenient(false).
Addendum: It looks like you're recognizing variously formatted Date strings. If you have to do entry, you might like looking at this example that extends InputVerifier.
Since an example of Apache FastDateFormat(click for the documentations of versions:2.6and3.5) is missing here, I am adding one for those who may need it. The key here is the pattern ZZ(2 capital Zs).
import java.text.ParseException
import java.util.Date;
import org.apache.commons.lang3.time.FastDateFormat;
public class DateFormatTest throws ParseException {
public static void main(String[] args) {
String stringDateFormat = "yyyy-MM-dd'T'HH:mm:ssZZ";
FastDateFormat fastDateFormat = FastDateFormat.getInstance(stringDateFormat);
System.out.println("Date formatted into String:");
System.out.println(fastDateFormat.format(new Date()));
String stringFormattedDate = "2016-11-22T14:30:14+05:30";
System.out.println("String parsed into Date:");
System.out.println(fastDateFormat.parse(stringFormattedDate));
}
}
Here is the output of the code:
Date formatted into String:
2016-11-22T14:52:17+05:30
String parsed into Date:
Tue Nov 22 14:30:14 IST 2016
Note: The above code is of Apache Commons' lang3. The class org.apache.commons.lang.time.FastDateFormat does not support parsing, and it supports only formatting. For example, the output of the following code:
import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
public class DateFormatTest {
public static void main(String[] args) throws ParseException {
String stringDateFormat = "yyyy-MM-dd'T'HH:mm:ssZZ";
FastDateFormat fastDateFormat = FastDateFormat.getInstance(stringDateFormat);
System.out.println("Date formatted into String:");
System.out.println(fastDateFormat.format(new Date()));
String stringFormattedDate = "2016-11-22T14:30:14+05:30";
System.out.println("String parsed into Date:");
System.out.println(fastDateFormat.parseObject(stringFormattedDate));
}
}
will be this:
Date formatted into String:
2016-11-22T14:55:56+05:30
String parsed into Date:
Exception in thread "main" java.text.ParseException: Format.parseObject(String) failed
at java.text.Format.parseObject(Format.java:228)
at DateFormatTest.main(DateFormatTest.java:12)
If date string is like 2018-07-20T12:18:29.802Z
Use this
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

Set the date and time manually - java

i'm trying to allow a customer to set the date and time they would want to make a reservation. The code which i have already completed creates lots of text in the text file when saved and crashes when you try to load it again.
This is my code for adding a reservation:
public static List<Reservation> addReservations(List<Reservation> reservations, List<Customer> customers) {
int newReservationId = Reservation.getNumberOfReservations() + 1;
String startString = readString("Enter Reservation date");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = sdf.parse(startString);
} catch (ParseException ex) {
Logger.getLogger(ProjectIncrement5.class.getName()).log(Level.SEVERE, null, ex);
}
Calendar dateTime = Calendar.getInstance();
dateTime.setTime(date);
listCustomers(customers, reservations);
int reservationCustomerId = readInt("Enter Customer Id", Customer.getNumberOfCustomers(), 1);
Customer reservationCustomer = customers.get(reservationCustomerId - 1);
Reservation res = new Reservation(newReservationId, dateTime, reservationCustomer);
reservations.add(res);
return reservations;
}
Reservation Class:
public class Reservation {
private int reservationId;
private Calendar dateTime;
private Customer customer;
private static int numberOfReservations = 0;
public Reservation() {
this.reservationId = 0;
this.dateTime = null;
this.customer = null;
numberOfReservations++;
}
public Reservation(int reservationId, Calendar dateTime, Customer customer) {
this.reservationId = reservationId;
this.dateTime = dateTime;
this.customer = customer;
numberOfReservations++;
}
public static int getNumberOfReservations() {
return numberOfReservations;
}
public int getReservationId() {
return reservationId;
}
public void setreservationId(int reservationId) {
this.reservationId = reservationId;
}
public Calendar getDateTime() {
return dateTime;
}
public Customer getCustomer()
{
return customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
public String setDateTime() {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(this.dateTime.getTime());
return dateString;
}
#Override
public String toString() {
return "Reservation id: " + getReservationId() + ", "
+ "Date/Time " + setDateTime() +
"customer: " + getCustomer();
}
}
This is what saves into the text file causing the error:
1:java.util.GregorianCalendar[time=851385600000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=1996,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=359,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]:4:<
I think the error occurs when getting the instance of the calendar but I'm am unsure of how to do it an easier way and fix this problem. Can anyone help?
The problem is you store the Calendar.toString() and not the real date into the text file.
Look at what you have posted:
firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=1996,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=359,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
That seems a correct date. I will say more, this seems a really complete date, with a lot of information doesn't??? ;)
I don't know how this date is supposed to be stored, but, for example, if you want to store date in format dd/MM/yyyy use this lines of code to check if it's a valid date (you already do this in your code).
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date = sdf.parse(startString);
After checking this, if the startString is valid, you don't need to create any Calendar instance just write startString to the text file.
UPDATE:
So in my Reservation class, instead of having: private Calendar dateTime I will take that out and just have: private String startString?
NOPE. The idea is:
In a Java class, store in a specific Object (Calendar is ok, but Date will be also)
In text file, send a human readable date via String.
Sorry but I can't be more specific, I can't execute your code because a lot of parts are missing (Customer, readInt, listCustomers etc...).
I accept Jordi Castilla's Answer, and also want to tell other option to do the same.
here you are saving the Date & Time to file using Calendar.toString() method, instead you could use following code
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String stringToFile = df.format(date);//you can send this string to the file which you want to store the Date & Time
Avoid legacy date-time classes
The modern approach uses java.time classes. Avoid the troublesome, confusing, and poorly-designed old legacy date-time classes such as Calendar.
LocalDate
Collect the date portion. Catch any DateTimeParseException in case of bad input.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
try {
LocalDate ld = LocalDate.parse( input , f ) ;
} catch ( DateTimeParseException e ) {
…
}
Seems like you are not explicitly tracking the time-of-day. So the LocalDate class suffices.
ISO 8601
When you serialize your data to a text file, you should be using standard ISO 8601 formats. These formats are designed to be practical & unambiguous, easy to parse by machine, easy to read by humans across cultures, and not assume proficiency in English.
The java.time classes use the standard formats by default when parsing and generating strings. Merely call toString to generate.
String output = ld.toString() ; // Ex: 2017-01-23
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Get all Fridays in a date Range in Java

I recently came across a task where i have to get all Fridays in a date range. I wrote a small piece of code and was surprised see some strange behaviour.
Below is my code:
public class Friday {
public static void main(String[]args){
String start = "01/01/2009";
String end = "12/09/2013";
String[] startTokens = start.split("/");
String[] endTokens = end.split("/");
Calendar startCal = new GregorianCalendar(Integer.parseInt(startTokens[2]),Integer.parseInt(startTokens[1])-1,Integer.parseInt(startTokens[0]));
Calendar endCal = new GregorianCalendar(Integer.parseInt(endTokens[2]),Integer.parseInt(endTokens[1])-1, Integer.parseInt(endTokens[0]));
int startYear = Integer.parseInt(startTokens[2]);
int endYear = Integer.parseInt(endTokens[2]);
int startWeek = startCal.get(Calendar.WEEK_OF_YEAR);
int endWeek = endCal.get(Calendar.WEEK_OF_YEAR);
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
// cal.setMinimalDaysInFirstWeek(7);
ArrayList<String> main = new ArrayList<String>();
while(startYear <= endYear ){
cal.set(Calendar.YEAR, startYear);
System.out.println(cal.getMinimalDaysInFirstWeek());
if(startYear == endYear){
main.addAll(getFridays(startWeek, endWeek, cal));
}
else{
main.addAll(getFridays(startWeek, 52, cal));
startWeek = 1;
}
startYear =startYear +1;
}
for(String s: main){
System.err.println(s);
}
}
public static ArrayList<String> getFridays(int startWeek, int endWeek, Calendar cal){
ArrayList<String> fridays = new ArrayList<String>();
while(startWeek <= endWeek){
cal.set(Calendar.WEEK_OF_YEAR, startWeek);
fridays.add(cal.getTime().toString());
startWeek = startWeek+1;
}
return fridays;
}
}
Now when I ran the code i noticed that Fridays of 2011 are missing. After some debugging and online browsing i figured that Calendar.WEEK_OF_YEAR is locale specific and I have to use setMinimalDaysInFirstWeek(7) to fix it.
So uncommented the related line in the above code.
From what I understood now first week of year should start from full week of year.
For example Jan 1 2010 is friday. But it should not show up in results as i configured it to treat that week begins from Jan 3rd. But Now i still see the Jan 1 as friday
I am very much confused. Can some one explain why it is happening?
These Stackoverflow articles helped me a bit:
Why dec 31 2010 returns 1 as week of year?
Understanding java.util.Calendar WEEK_OF_YEAR
Here is an easier method, using the wonderful http://www.joda.org/joda-time/ library:
String start = "01/01/2009";
String end = "12/09/2013";
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);
List<DateTime> fridays = new ArrayList<>();
while (startDate.isBefore(endDate)){
if ( startDate.getDayOfWeek() == DateTimeConstants.FRIDAY ){
fridays.add(startDate);
}
startDate = startDate.plusDays(1);
}
at the end of this, you'd have the fridays in the fridays array. Simple?
Or if you want to speed things up, once you have gotten a friday, you can switch from using days, to using weeks:
String start = "01/01/2009";
String end = "12/09/2013";
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);
List<DateTime> fridays = new ArrayList<>();
boolean reachedAFriday = false;
while (startDate.isBefore(endDate)){
if ( startDate.getDayOfWeek() == DateTimeConstants.FRIDAY ){
fridays.add(startDate);
reachedAFriday = true;
}
if ( reachedAFriday ){
startDate = startDate.plusWeeks(1);
} else {
startDate = startDate.plusDays(1);
}
}
First off, I would not bother with weeks. Set the Calendar to the beginning of the range, and figure out which DOW it is, then increment to get to the next Friday, then simply loop adding 7 days until you are at the end of the range.
Actually, since you are always only going forward, should be something like:
int daysToAdd = FridayDOW - currentDOW;
if (daysToAdd < 0) daysToAdd += 7;
Date startDate = currentDate.add(Calendar.DAYS, daysToAdd);
Yeah, like that.
Ok, actually, for kicks, here it is in Java 8:
#Test
public void canFindAllFridaysInRange(){
start = LocalDate.of(2013, 5, 10);
end = LocalDate.of(2013, 8,30);
DayOfWeek dowOfStart = start.getDayOfWeek();
int difference = DayOfWeek.FRIDAY.getValue() - dowOfStart.getValue();
if (difference < 0) difference += 7;
List<LocalDate> fridaysInRange = new ArrayList<LocalDate>();
LocalDate currentFriday = start.plusDays(difference);
do {
fridaysInRange.add(currentFriday);
currentFriday = currentFriday.plusDays(7);
} while (currentFriday.isBefore(end));
System.out.println("Fridays in range: " + fridaysInRange);
}
Got to love the new date classes!! Of course a lambda would condense this further.
tl;dr
someLocalDate.with( // Date-only value without time-of-day and without time zone, represented by `LocalDate` class.
TemporalAdjusters.nextOrSame ( DayOfWeek.FRIDAY ) ) // Moving from one `LocalDate` object to another, to find the next Friday unless the starting date is already a Friday.
) // Return a `LocalDate` object.
java.time
The other Answers are outdated. The old java.util.Date/.Calendar classes have been supplanted in Java 8 and later by the new java.time framework. Joda-Time library is excellent, continues to be maintained, and even inspired java.time. But the Joda-Time team recommends moving on to java.time as soon as is convenient.
LocalDate
The java.time classes include LocalDate for a date-only value without time-of-day nor time zone. See Tutorial.
First parse your input strings to get LocalDate objects.
String inputStart = "01/01/2009";
String inputStop = "12/09/2013"; // 258 Fridays.
// String inputStop = "01/01/2009"; // 0 Friday.
// String inputStop = "01/02/2009"; // 1 Friday.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "MM/dd/yyyy" );
LocalDate start = LocalDate.parse ( inputStart , formatter );
LocalDate stop = LocalDate.parse ( inputStop , formatter );
In your own code, try-catch for exception in case of bad inputs. And verify that stop is indeed the same or later than start.
TemporalAdjusters
The java.time framework includes the TemporalAdjuster interface as a way of shifting date-time values. For example, getting the next or same Friday for any particular date. On your starting date, call with(TemporalAdjuster adjuster) and pass a pre-defined implementation of a TemporalAdjuster from the class TemporalAdjusters (note the plural s). See Tutorial.
List<LocalDate> fridays = new ArrayList<> (); // Collect each Friday found.
LocalDate nextOrSameFriday = start.with ( TemporalAdjusters.nextOrSame ( DayOfWeek.FRIDAY ) );
// Loop while we have a friday in hand (non-null) AND that friday is not after our stop date (isBefore or isEqual the stop date).
while ( ( null != nextOrSameFriday ) & ( ! nextOrSameFriday.isAfter ( stop ) ) ) {
fridays.add ( nextOrSameFriday ); // Remember this friday.
nextOrSameFriday = nextOrSameFriday.plusWeeks ( 1 ); // Move to the next Friday, setting up for next iteration of this loop.
}
Dump to console.
System.out.println ( "From: " + start + " to: " + stop + " are " + fridays.size () + " Fridays: " + fridays );
From: 2009-01-01 to: 2013-12-09 are 258 Fridays: [2009-01-02, 2009-01-09, 2009-01-16, 2009-01-23, 2009-01-30, 2009-02-06, 2009-02-13, 2009-02-20, 2009-02-27, 2009-03-06, 2009-03-13, 2009-03-20, 2009-03-27, 2009-04-03, 2009-04-10, 2009-04-17, 2009-04-24, 2009-05-01, 2009-05-08, 2009-05-15, 2009-05-22, 2009-05-29, 2009-06-05, 2009-06-12, 2009-06-19, 2009-06-26, 2009-07-03, 2009-07-10, 2009-07-17, 2009-07-24, 2009-07-31, 2009-08-07, 2009-08-14, 2009-08-21, 2009-08-28, 2009-09-04, 2009-09-11, 2009-09-18, 2009-09-25, 2009-10-02, 2009-10-09, 2009-10-16, 2009-10-23, 2009-10-30, 2009-11-06, 2009-11-13, 2009-11-20, 2009-11-27, 2009-12-04, 2009-12-11, 2009-12-18, 2009-12-25, 2010-01-01, 2010-01-08, 2010-01-15, 2010-01-22, 2010-01-29, 2010-02-05, 2010-02-12, 2010-02-19, 2010-02-26, 2010-03-05, 2010-03-12, 2010-03-19, 2010-03-26, 2010-04-02, 2010-04-09, 2010-04-16, 2010-04-23, 2010-04-30, 2010-05-07, 2010-05-14, 2010-05-21, 2010-05-28, 2010-06-04, 2010-06-11, 2010-06-18, 2010-06-25, 2010-07-02, 2010-07-09, 2010-07-16, 2010-07-23, 2010-07-30, 2010-08-06, 2010-08-13, 2010-08-20, 2010-08-27, 2010-09-03, 2010-09-10, 2010-09-17, 2010-09-24, 2010-10-01, 2010-10-08, 2010-10-15, 2010-10-22, 2010-10-29, 2010-11-05, 2010-11-12, 2010-11-19, 2010-11-26, 2010-12-03, 2010-12-10, 2010-12-17, 2010-12-24, 2010-12-31, 2011-01-07, 2011-01-14, 2011-01-21, 2011-01-28, 2011-02-04, 2011-02-11, 2011-02-18, 2011-02-25, 2011-03-04, 2011-03-11, 2011-03-18, 2011-03-25, 2011-04-01, 2011-04-08, 2011-04-15, 2011-04-22, 2011-04-29, 2011-05-06, 2011-05-13, 2011-05-20, 2011-05-27, 2011-06-03, 2011-06-10, 2011-06-17, 2011-06-24, 2011-07-01, 2011-07-08, 2011-07-15, 2011-07-22, 2011-07-29, 2011-08-05, 2011-08-12, 2011-08-19, 2011-08-26, 2011-09-02, 2011-09-09, 2011-09-16, 2011-09-23, 2011-09-30, 2011-10-07, 2011-10-14, 2011-10-21, 2011-10-28, 2011-11-04, 2011-11-11, 2011-11-18, 2011-11-25, 2011-12-02, 2011-12-09, 2011-12-16, 2011-12-23, 2011-12-30, 2012-01-06, 2012-01-13, 2012-01-20, 2012-01-27, 2012-02-03, 2012-02-10, 2012-02-17, 2012-02-24, 2012-03-02, 2012-03-09, 2012-03-16, 2012-03-23, 2012-03-30, 2012-04-06, 2012-04-13, 2012-04-20, 2012-04-27, 2012-05-04, 2012-05-11, 2012-05-18, 2012-05-25, 2012-06-01, 2012-06-08, 2012-06-15, 2012-06-22, 2012-06-29, 2012-07-06, 2012-07-13, 2012-07-20, 2012-07-27, 2012-08-03, 2012-08-10, 2012-08-17, 2012-08-24, 2012-08-31, 2012-09-07, 2012-09-14, 2012-09-21, 2012-09-28, 2012-10-05, 2012-10-12, 2012-10-19, 2012-10-26, 2012-11-02, 2012-11-09, 2012-11-16, 2012-11-23, 2012-11-30, 2012-12-07, 2012-12-14, 2012-12-21, 2012-12-28, 2013-01-04, 2013-01-11, 2013-01-18, 2013-01-25, 2013-02-01, 2013-02-08, 2013-02-15, 2013-02-22, 2013-03-01, 2013-03-08, 2013-03-15, 2013-03-22, 2013-03-29, 2013-04-05, 2013-04-12, 2013-04-19, 2013-04-26, 2013-05-03, 2013-05-10, 2013-05-17, 2013-05-24, 2013-05-31, 2013-06-07, 2013-06-14, 2013-06-21, 2013-06-28, 2013-07-05, 2013-07-12, 2013-07-19, 2013-07-26, 2013-08-02, 2013-08-09, 2013-08-16, 2013-08-23, 2013-08-30, 2013-09-06, 2013-09-13, 2013-09-20, 2013-09-27, 2013-10-04, 2013-10-11, 2013-10-18, 2013-10-25, 2013-11-01, 2013-11-08, 2013-11-15, 2013-11-22, 2013-11-29, 2013-12-06]
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
This code will print all dates having Friday.
public class Friday {
public static void main(String[] args) throws ParseException {
String start = "01/01/2013";
String end = "12/01/2013";
SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
Calendar scal=Calendar.getInstance();
scal.setTime(dateFormat.parse(start));
Calendar ecal=Calendar.getInstance();
ecal.setTime(dateFormat.parse(end));
ArrayList<Date> fridayDates=new ArrayList<>();
while(!scal.equals(ecal)){
scal.add(Calendar.DATE, 1);
if(scal.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY){
fridayDates.add(scal.getTime());
}
}
System.out.println(fridayDates);
}
}
Here a solution based on new stream-features of Java-8 and using my library Time4J (v4.18 or later):
String start = "01/01/2009";
String end = "12/09/2013";
ChronoFormatter<PlainDate> f =
ChronoFormatter.ofDatePattern("dd/MM/yyyy", PatternType.CLDR, Locale.ROOT);
PlainDate startDate =
f.parse(start).with(PlainDate.DAY_OF_WEEK.setToNextOrSame(Weekday.FRIDAY));
PlainDate endDate = f.parse(end);
Stream<PlainDate> fridays =
DateInterval.stream(Duration.of(1, CalendarUnit.WEEKS), startDate, endDate);
fridays.forEachOrdered(System.out::println);
// output
2009-01-02
2009-01-09
...
2013-08-30
2013-09-06
// other example: list of fridays in ISO-8601-format
List<String> result =
DateInterval.between(startDate, endDate)
.stream(Duration.of(1, CalendarUnit.WEEKS))
.map((date) -> date.toString()) // or maybe use dd/MM/yyyy => f.format(date)
.collect(Collectors.toList());
By the way, Java-9 will offer a similar solution (but with exclusive end date boundary), see also this enhancement-issue.
with Lamma Date :
List<Date> fridays = Dates.from(2015, 12, 1).to(2016, 1, 1).byWeek().on(DayOfWeek.FRIDAY).build();
for (Date friday: fridays) {
System.out.println(friday);
}
public static List<Date> getWeekNumberList(Date currentMonthDate) {
List<Date> dates = new ArrayList<>(10);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(currentMonthDate);
startCalendar.set(Calendar.DAY_OF_MONTH,
startCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(currentMonthDate);
endCalendar.set(Calendar.DAY_OF_MONTH,
endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date enddate = endCalendar.getTime();
while (startCalendar.getTime().before(enddate)) {
if (startCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
Date result = startCalendar.getTime();
dates.add(result);
startCalendar.add(Calendar.WEEK_OF_MONTH, 1);
} else {
startCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
return dates;
}
Using Java 8+
LocalDate s= LocalDate.now();
LocalDate e= LocalDate.now().plusMonths(5);
List<LocalDate> dates2 = s.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)).datesUntil(e, Period.ofWeeks(1)).collect(Collectors.toList());
dates2.forEach(x->System.out.println(x));

How can I convert Json Date to Java Date

I am trying to convert Json date string to java date format. However, it gives error when it comes to "return df.parse( tarih )" line.
JSON :
{"DateFrom":"\/Date(1323087840000+0200)\/"}
Java code :
private Date JSONTarihConvert(String tarih) throws ParseException
{
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
if ( tarih.endsWith( "Z" ) ) {
tarih = tarih.substring( 0, tarih.length() - 1) + "GMT-00:00";
} else {
int inset = 6;
String s0 = tarih.substring( 6, tarih.length()-1 - inset );
String s1 = tarih.substring( tarih.length()- inset,tarih.length()-2 );
tarih = s0 + "GMT" + s1;
}
return df.parse( tarih );
}
When I call this method, tarih parameter is: /Date(1323087840000+0200)/
As you're interested in a Date object and the JSON occurs to me to be a unix timestamp.
Therefore I'd recommend you the Date(long milliseconds) constructor :)
private Date JSONTarihConvert(String tarih) throws ParseException{
long timestamp = getTimeStampFromTarih(tarih);
return new Date(timestamp);
}
Where getTimeStampFromTarih extracts the milliseconds before the occurrence of "+"
This will work surely
String date = "/Date(1376841597000)/";
Calendar calendar = Calendar.getInstance();
String datereip = date.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(datereip);
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar.getTime().toString("dd-MMM-yyyy h:mm tt"));//It Will Be in format 29-OCT-2014 2:26 PM
First replace string like this :
String str= ConvertMilliSecondsToFormattedDate(strDate.replace("/Date(","").replace(")/", ""));
Then Convert it like this:
public static String ConvertMilliSecondsToFormattedDate(String milliSeconds){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(milliSeconds));
return simpleDateFormat.format(calendar.getTime());
}
You will Need to type cast date:
String rawdate = "/Date(1995769286000)/";
Calendar calendarins = Calendar.getInstance();
String datereip = rawdate.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(datereip);
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar.getTime().toString("dd-MMM-yyyy h:mm tt"));
Unless you have a reason not to, you should be using a parser to serialize and de-serialize objects. Like Jackson parser.
java.time
This is the modern answer (since 2014).
First I want to make sure the timestamp I have really lives up to the format I expect. I want to make sure if one day it doesn’t, I don’t just pretend and the user will get incorrect results without knowing they are incorrect. So for parsing the timestamp string, since I didn’t find a date-time format that would accept milliseconds since the epoch, I used a regular expression:
String time = "/Date(1479974400000-0800)/";
Pattern pat = Pattern.compile("/Date\\((\\d+)([+-]\\d{4})\\)/");
Matcher m = pat.matcher(time);
if (m.matches()) {
Instant i = Instant.ofEpochMilli(Long.parseLong(m.group(1)));
System.out.println(i);
}
This prints:
2016-11-24T08:00:00Z
It is not clear to me whether you need to use the zone offset and for what purpose. But since we’ve got it, why not retrieve it from the matcher and use it for forming an OffsetDateTime, a date and time with UTC offset. Here’s how:
ZoneOffset zo = ZoneOffset.of(m.group(2));
OffsetDateTime odt = OffsetDateTime.ofInstant(i, zo);
System.out.println(odt);
2011-12-05T14:24+02:00
If you need an old-fashioned java.util.Date for some legacy API:
System.out.println(Date.from(i));
Or if using the backport mentioned in the links below:
System.out.println(DateTimeUtils.toDate(i));
On my computer it prints
Mon Dec 05 13:24:00 CET 2011
The exact output will depend on your time zone.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

Categories