SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String startDateStr = stripQuotes("2017/08/01 15:18:01");
Date startDateDate;
Calendar startDate = null;
try {
startDateDate = dateFormat.parse(startDateStr)
startDate = Calendar.getInstance();
startDate.setTime(startDateDate);
} catch (ParseException ex) {
Logger.getLogger(AttendanceController.class.getName()).log(Level.SEVERE, null, ex);
}
Honey honey1 = new Honey(1,"ok","ok2",startDate);

- 5,919
- 11
- 35
- 51

- 3
- 1
-
5Works fine for me - what does `stripQuotes` do? You should also consider using the newer Java Timer API available in Java 8 ... or just about any other date/time API (like JodaTime) – MadProgrammer Nov 21 '17 at 00:28
-
IMHO you shouldn’t bother with the long outdated classes `SimpleDateFormat`, `Calendar` and `Date`. `java.time`, the modern Java date and time API also known as JSR-310, is so much nicer to work with. You can use it in Java 6 and later (in Java 6 and 7 through the [ThreeTen Backport](http://www.threeten.org/threetenbp/)). – Ole V.V. Nov 21 '17 at 07:19
-
I see no way your code could produce a `startDate` here in November except on a computer with the clock set three months off. Could you [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), please? – Ole V.V. Nov 21 '17 at 07:39
-
Sorry guys, it seems that the specific code works perfectly. On the other hand, i was using streams in another method by just giving the (startdate)/ variable the current date again. I had to use the debugger to get it sorted. Thanks a lot for your feedback, Appreciate it! – vinnysingh Nov 23 '17 at 01:17
2 Answers
tl;dr
LocalDateTime.parse(
"2017/08/01 15:18:01" ,
DateTimeFormatter.ofPattern( "uuuu/MM/dd HH:mm:ss" , Locale.US )
).atOffset( ZoneOffset.UTC )
Legacy classes
As others stated, your code should be working as expected.
// Old outmoded way using troublesome legacy classes.
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse( input ) ;
Calendar cal = Calendar.getInstance() ;
cal.setTime( date ) ;
System.out.println( "date.toString(): " + date ) ;
You can see that code run live at IdeOne.com.
date.toString(): Tue Aug 01 15:18:01 GMT 2017
You have a bigger problem. You are using terribly troublesome old date-time classes that are now legacy, supplanted by the java.time classes. Avoid these old classes like the Plague.
ISO 8601
By the way, you are using a less-than-optimal format for representing a date-time as a String. Instead use the standard ISO 8601 formats. The java.time classes use the ISO 8601 formats by default when parsing/generating strings. You can see so in next example.
And specify a time zone where one is intended. Omitting the offset or zone leads to confusion, errors, and pain.
java.time
Let’s rewrite that code the modern way.
// New modern way in Java 8 and later.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd HH:mm:ss" , Locale.US ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
Your example code in the Questions suggests the date-time represented by the input string was intended to represent a moment in UTC (an offset-from-UTC of zero). The LocalDateTime
object we have above has no zone or offset, and so does not represent a point on the timeline. This object has no definite meaning until you assign an offset/zone.
// If we assume this date-time was meant to be UTC.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
System.out.println( "odt.toString(): " + odt ) ;
You can see that code run live at IdeOne.com.
odt.toString(): 2017-08-01T15:18:01Z

- 303,325
- 100
- 852
- 1,154
I just ran this code and works fine for me. startDate.getTime()
returns Tue Aug 01 15:18:01 PDT 2017
, which is as expected.
Only thing is you're missing a semicolon at the end of line startDateDate = dateFormat.parse(startDateStr)
.
This might also help you: Set the Calendar to a specific date

- 16
- 1