Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Converting Date Object from one format to another

  • 23-02-2016 06:47PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 793 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 793 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 793 ✭✭✭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