Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Questions regarding date manipulation and semi-advanced SQL queries

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Questions regarding date manipulation and semi-advanced SQL queries

    So I'm about ready to drive my car off a bridge and really need an outside opinion on how to do this, I have been struggling for days to find any kind of valid solution and feel as though it's simply all my pent up stress holding me back. That being said any help is appreciated.

    Preface:
    My application handles customers in a sql database and users can select a customer and upload an appointment to another table.

    Where I am running into serious mental blocks is converting the dates properly, as well as querying the SQL. I am using JavaFX datepicker, which returns a Date() in the form 'YYYY-MM-DD', example for today would be '11/25/2018'. Appointments are stored in UTC, which leads me to my first problem.

    If an employee selects a date, say December 1st, 2018, this date needs to be converted to UTC and the full 24 hours need to be queried from the database.


    Question 1:
    So my idea was get the Date() object from datepicker which is in the above form, then adding " 00:00:00" to it and attempting to convert to UTC, but I have no idea how to do this and can't find anything relevant, how would you go about doing this.

    Question 2:
    I need do the same as above, but with the Date() + 1 full day, so if the original was December 1, 2018 at 00:00:00 my time, then this would be December 2, 2018 at 00:00:00 my time, I can't find any real way to add a day to a Date() object properly.

    Question 3:
    The database holds a start business time, in UTC just for simplicity say its "00:00:00" and extends 8 hours so the end time would be "08:00:00", how do I return all appointment entries that fall between these times from question 2, assuming the start/end times are in datetime format.

    Please, I don't know who else to ask. I have an application that is crippled because I simply can't figure out a solution to such a stupid simple question.


    Here's my attempt at question 1/2 with output

    test("2018-12-01");
     
        private static void test(String date){
            try{
                DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date startDate = utcFormat.parse(date + " 00:00:00");
                System.out.println(startDate);
     
                LocalDateTime today = convertToLocalDateTimeViaSqlTimestamp(startDate);
                today = today.plusDays(1);
     
                System.out.println(convertToDateViaSqlTimestamp(today));
            }catch(Exception e){
     
            }
     
        }
        private static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
            return java.sql.Timestamp.valueOf(dateToConvert);
        }
        private static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) {
            return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime();
        }
    Output:
    Fri Nov 30 16:00:00 PST 2018
    2018-12-01 16:00:00.0
    Last edited by TheAnonyMoose; November 26th, 2018 at 02:25 AM.

Similar Threads

  1. Splitting date string by date and time and assigning it to 2 variables
    By KaranSaxena in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 17th, 2014, 05:58 AM
  2. SQL statement is not executed! java.sql.SQLException: General error
    By ppz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 20th, 2014, 05:36 PM
  3. Overriding a method, semi complicatedly.
    By Tron22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2012, 07:44 AM
  4. Help running SQL queries in Ubuntu
    By Mic[RSA] in forum Java IDEs
    Replies: 0
    Last Post: June 29th, 2010, 12:53 PM
  5. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM

Tags for this Thread