Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Converting Date Object from one format to another

Options
  • 23-02-2016 6:47pm
    #1
    Registered Users Posts: 262 ✭✭


    See below output. I'm looking to convert the former date object to be in the latters format. Is there a way to do this in Java

    START DATE IS

    Sat Jan 17 20:30:42 GMT 1970

    AND WE NEED IT TO BE

    2016-02-12 23:56:15


Comments

  • Registered Users Posts: 776 ✭✭✭pillphil


    Is it a date object or a string?


    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = sdf.parse(time);
    String formattedTime = output.format(d);

    https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html


  • Registered Users Posts: 262 ✭✭guylikeme


    pillphil wrote: »
    Is it a date object or a string?


    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = sdf.parse(time);
    String formattedTime = output.format(d);

    https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

    String. Thanks above seems to help.

    What of i only have a long like below though...

    public static String makeDateFromLong(long secondsSinceEpoch) {
    Date date = new Date();
    String dateAsString = "";
    try {
    date.setTime(secondsSinceEpoch);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
    //date = simpleDateFormat.parse(date);
    dateAsString = simpleDateFormat.format(date);
    } catch (final Exception e) {
    e.printStackTrace();
    }
    log.debug ("Returning " + dateAsString);
    return dateAsString;
    }

    Seems to output "Returning 1970-01-17 20:30:43" when i pass it the secondsSinceEpoch of a date this year.

    Im geussing that the commented parse bit needs a String but what to pass it


  • Registered Users Posts: 776 ✭✭✭pillphil


    guylikeme wrote: »
    String. Thanks above seems to help.

    What of i only have a long like below though...

    public static String makeDateFromLong(long secondsSinceEpoch) {
    Date date = new Date();
    String dateAsString = "";
    try {
    date.setTime(secondsSinceEpoch);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
    //date = simpleDateFormat.parse(date);
    dateAsString = simpleDateFormat.format(date);
    } catch (final Exception e) {
    e.printStackTrace();
    }
    log.debug ("Returning " + dateAsString);
    return dateAsString;
    }

    Seems to output "Returning 1970-01-17 20:30:43" when i pass it the secondsSinceEpoch of a date this year.

    Im geussing that the commented parse bit needs a String but what to pass it

    I think you are passing the seconds since epoch when you should be passing milliseconds since epoch.


  • Registered Users Posts: 262 ✭✭guylikeme


    You may be right. However, when i pass the milliseconds, i still get an odd date.


    Outputting this

    Raw date is 2016-02-12 23:56:15
    1452642975000milliseconds since epoch for original start value
    555000
    After adding the two, start is 1452643530000
    Date IS 2016-01-13 00:05:30 AND WE NEED IT TO BE 2016-02-12 23:56:15

    Code is as follows in main

    long startSinceEpoch = DateTimeUtil.getMillisSinceEpoch(start);
    logger.debug(startSinceEpoch + "milliseconds since epoch for original start value");
    long startMillisToAdjust = startSecondsToAdjust * 1000;
    logger.debug("" + startMillisToAdjust);
    long startAdjustedMillisSinceEpoch = startSinceEpoch + startMillisToAdjust;
    logger.debug("After adding the two, start is " + startAdjustedMillisSinceEpoch);
    String newStartDateFormatted = DateTimeUtil.makeDateFromLong(startAdjustedMillisSinceEpoch);
    logger.debug("Date IS " + newStartDateFormatted + " AND WE NEED IT TO BE " + Constants.START_TIME);


    The methods to parse date etc below

    public static String makeDateFromString(String rawDate){
    log.debug("Raw date is " + rawDate);
    Date date = null;
    String output = "";
    try {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
    date = simpleDateFormat.parse(rawDate);
    output = simpleDateFormat.format(date);
    } catch (final Exception e) {
    e.printStackTrace(); }
    return output;
    }
    public static long getMillisSinceEpoch(String rawDate){
    log.debug("Raw date is " + rawDate);
    Date date = null;
    long secondsSinceEpoch = 0;
    try {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
    date = simpleDateFormat.parse(rawDate);

    } catch (final Exception e) {
    e.printStackTrace(); }
    secondsSinceEpoch = date.getTime();
    return secondsSinceEpoch;
    }

    public static String makeDateFromLong(long secondsSinceEpoch) {
    Date date = new Date();
    String dateAsString = "";
    try {
    date.setTime(secondsSinceEpoch);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateAsString = simpleDateFormat.format(date);
    date = simpleDateFormat.parse(dateAsString);


    } catch (final Exception e) {
    e.printStackTrace();
    }
    return dateAsString;
    }


  • Registered Users Posts: 776 ✭✭✭pillphil


    The use of DD vs dd is causing the problem. D is the day in year, d is the day in month.


  • Advertisement
Advertisement