• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

connecting to an Apache Derby database

 
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK


so to round to the nearest minute...........

= Round((calendar2+500)/60000)*60000

Is that the idea ?
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this looks elegant................


setup for nearest hour....I shall change it to the nearest 10 minutes
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am trying this approach...........


However I get.................

Time               Messages                                      
2021-08-09 00:50:01 date_current: 2021-08-09; time_current: 0:40:00

questions:-

why has minutes gone from 50 to 40 ?
why is the format H:mm:ss instead of HH:mm:ss ?
 
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob, you might want to make an SSCCE (<-- that's a link). Making and posting a small executable that demonstrates the problem gives you the opportunity to see what's wrong straight away, or if not, it allows us to compile it and run it in order to help you debug it. With your last post we don't have enough information to respond.
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all code...........


Time               Messages                                      
2021-08-09 00:50:01 date_current: 2021-08-09; time_current: 0:40:00
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Matthews wrote:all code...........


Time               Messages                                      
2021-08-09 00:50:01 date_current: 2021-08-09; time_current: 0:40:00

On line 5 you add 30 seconds and then again on line 19 you add 30 seconds. Line 13 you format LocalDate and then throw the String away. Line 23 you don't format the minutes to be 2 digits wide (use String.format()). Ditto for hours. After lines 19-21 you don't do anything further with "calendar2".
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is "askBar" in a specific time zone? Or is it whatever time zone the machine happens to be in?
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have expected both date_current and time_current to come from the same source, instead one comes from now() and one comes from "askBar" which may be a problem. Why not use now() for both of them?
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calendar calendar2 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code with adjustments.................


Time               Messages                                                        
2021-08-09 02:50:00 Inserted new record into Prices:- Date/Time: 2021-08-09, 2:40:00

clearly my formatting is NOT working and still getting 40 instead of 50
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling format() but throwing away the returned String.
Try
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carey

Time               Messages                                                        
2021-08-09 03:10:01 Inserted new record into Prices:- Date/Time: 2021-08-09, 03:00:00

still moving from 3:10 down to 3:00 ???
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Matthews wrote:Thanks Carey

Time               Messages                                                        
2021-08-09 03:10:01 Inserted new record into Prices:- Date/Time: 2021-08-09, 03:00:00

still moving from 3:10 down to 3:00 ???

I don't see the problem. You'd have to print out the contents of askBar as well. I'm guessing that askBar and the timestamp used aat the beginning of the print statement come from different origins.
 
Marshal
Posts: 26750
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Matthews wrote:2021-08-09 03:10:01 Inserted new record into Prices:- Date/Time: 2021-08-09, 03:00:00

still moving from 3:10 down to 3:00 ???



You haven't shown us that code which prints that string. And also, you haven't explained why the two timestamps in that output have to be the same. If they do have to be the same, then the code which prints that line should just print the same data there, instead of making you do handstands to make the different data items have the same value.
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
current_date comes from LocalTime.now()

current_time comes from


The problem (see last post)

the time is 3:10:01 when the new record is added
but the new record has a time of 3:00:00 whereas I expect it to be 3:10:00
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It "appears" that one comes from askBar and the other from now(). Is that correct?
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
askBar yields ASK_price only
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Matthews wrote:this looks elegant................


setup for nearest hour....I shall change it to the nearest 10 minutes

Are you still doing something like this? I don't see it in the current code you're posting.
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - not using that code
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the scenario..................

every ten minutes my program does things.........

each ten minutes I grab two prices and write out a new record..........

e.g. at 4:40 my console shows date and time - time as 4:40:00 or maybe 4:40:01

I wish the new record to store date and time as strings namely date string and time string = 4:40:00

At the moment I seem to end up with a time of 4:30:00 ???
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why aren't you getting time_current from now() like you are with date_current?
 
Saloon Keeper
Posts: 24214
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems WAY over-complicated.

If you want to log the current time/date rounded up to the next 10 minutes, you don't need elaborate Calendar and Localtime mechanims. The default constructor

Will give you the current time/date to an accuracy of milliseconds, which is more than what you need.

Do the rounding magic I outlined earller to round up to the 10-minite mark, then use the rounded "now" as a constructor argument to a new Date(now);

You can use that Date object directly to write your timestamp out to the database.
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rounded to the 'nearest' 10 minutes
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I totally agree - should be very simple

BUT using the Calendar approach which I can understand and which doesn't require me to work in large numbers etc.

Date and prices are fine but NOT time!

I still do NOT understand why at time 3:50:01 (on the console) I only get 3:40:00 as a time to use when writing out a new record



You have not explained why this is ?
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't explained it either. Why are you getting the time from two different places?
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting the date and the time from 2 different places

Sure - that is silly, but put that to one side at the moment..........

The date is good from either

It's the time I don't follow
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the time good from either? How do you know?
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using Calendar calendar2

Time               Messages                                        
2021-08-09 21:00:00 date_current: 2021-08-09; time_current: 20:50:00

question: why, when the time is 21:00:00 on the console does calendar2 yield 20:50:00 as the time ?
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a complete SSCCE with code to do the rounding and code to demonstrate it in action. It is complete. You can compile it and run it for yourself. This is what I've asked you to provide to us but you have not. You (and we) are flying blind. You post code with critical pieces missing that are necessary for debugging. If you are having trouble with the console not equaling the Calendar, then write an SSCCE to demonstrate that.
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Carey

With your rounding code I get the answer I want

Still don't understand why Calendar doesn't give me the same time answer and why rounding is necessary but enough

Many thanks
Bob M
Dunedin
New Zealand
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad it worked for you.

I find it HIGHLY suspect that you were thinking of rounding to the nearest 10 minutes AND Calendar HAPPENS to be OFF by 10 MINUTES. It's almost as if you left some code in play that you didn't mean to.

If the time had been off by a multiple of hours then I would have suspected a time zone issue. But 10 minutes?
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
latest code.................


example of output

Time               Messages                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
2021-08-10 04:00:00 Inserted new record into Prices:- Date/Time: 2021-08-10, 04:00:00                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                   
2021-08-10 03:50:00 Inserted new record into Prices:- Date/Time: 2021-08-10, 03:50:00                                                                                                                                                                                                                                                                                                                                                                                                                            
 
Carey Brown
Saloon Keeper
Posts: 8458
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 17 & 23 don't do what you think they do. Read the Javadoc for format().
 
Bob Matthews
Master Rancher
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are correct Carey
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic