How to convert String input with milliseconds to Date

convert milliseconds to date in java
convert milliseconds to date android
how to convert milliseconds to date in javascript
java milliseconds to date online
convert long to date android
java convert milliseconds to time
get current date in milliseconds java
long to date java 8

I have a java method which needs to convert a Date from one input String format to another. The input and output formats must retain all millisecond detail, as this is for a process tracking solution where timestamps will often be just milliseconds apart.

The SimpleDateFormat seems to be losing all millisecond data, and just returning random numbers for the seconds and milliseconds.

input date String: "05/23/2013 12:32:13.45133" input date format: "MM/dd/yyyy HH:mm:ss.SSSSS" output date format: "yyyyMMddHHmmssSSS"

Given the above input date and format, my resulting Date String is: 20130523123258133. This is obviously completely off from the input seconds.

The input format may vary, but the output format must always be the same.

Below is the current method:

private long dateSequencer(String inputDt, String inputFormat) {
    long result = 0;
    try {
        // Convert input date into yyyyMMddHHmmssSSS format for sequencing
        SimpleDateFormat dtFrmt = new SimpleDateFormat(inputFormat);
        Date dt = new Date();
        dt = dtFrmt.parse(inputDt);
        // input String converted to Date, now reformat to Long
        dtFrmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String reFrmt = dtFrmt.format(dt);
        result = Long.parseLong(reFrmt);
    } catch (Exception ex) {
        System.out.println("++++ Exception converting dateSequencer: " + ex.toString());
    }
    System.out.print("+++ABM dateSeq: [inputDt=" + inputDt + "] [inputFormat=" + inputFormat + "] [result=" + result +"]");
    return result;
}

Log file entry from runtime:

+++ABM dateSeq: [inputDt=05/23/2013 12:32:13.45133] [inputFormat=MM/dd/yyyy HH:mm:ss.SSSSS] [result=20130523123258133]

Since the input format may change, I cannot manually parse the input String to rely on a specific pattern. This is why I am allowing the pattern to be passed, and using a SimpleDateFormat to convert to Date, then back to String.

How can I do this, reliably? Is there a more efficient way to convert a given input Date string, to a standard format?

Thanks!

If you can assume that in every case month, day, minute, second have 2 digits, year has 4 digits and milliseconds have 3 digits and just the pattern differs, you can simply do the following:

public static long convert(String inputDt, String inputFormat) {
    int dd = inputFormat.indexOf("dd");
    int MM = inputFormat.indexOf("MM");
    int yyyy = inputFormat.indexOf("yyyy");
    int HH = inputFormat.indexOf("HH");
    int mm = inputFormat.indexOf("mm");
    int ss = inputFormat.indexOf("ss");
    int SSS = inputFormat.indexOf("SSS");
    return Long.valueOf(inputDt.substring(yyyy,yyyy+4) + inputDt.substring(MM, MM+2) + inputDt.substring(dd, dd+2) + inputDt.substring(HH, HH+2)
            + inputDt.substring(mm, mm+2) + inputDt.substring(ss, ss+2) + inputDt.substring(SSS,SSS+3));        
}

convert("05/23/2013 12:32:13.133", "MM/dd/yyyy HH:mm:ss.SSS") yields 20130523123213133

How convert String to dateTime with milliseconds, With that being said, your format string to parse the given date should Your input string lacks any indication of time zone or offset-from-UTC. Convert time into date object and store it into new variable date. Convert the date object’s contents into a string using date.toString() function; Example 1: This example first gets the milliseconds of the current date and time, Then using that value to get the date by Date() method.

Your input date String: "05/23/2013 12:32:13.45133" contains 5 digits for milliseconds. And It's not possible... There must be 3 digits. So 45133 milliseconds is equals to 45 seconds and 133 millis. Your input date string is the date "05/23/2013 12:32:58.133"

Program to Convert Milliseconds to a Date Format in Java , The SimpleDateFormat class helps in formatting and parsing of data. We can change the date from one format to other. It allows the user to interpret string date​  4 Answers4. You don't have a Date, you have a String representation of a date. You should convert the String into a Date and then obtain the milliseconds. To convert a String into a Date and vice versa you should use SimpleDateFormat class.

How to convert milliseconds to Date in Java - tutorial , By the ways this is next in Date related article, we have already discussed How to convert String to Date and How to get current month, year and  The OP was about converting from a number of milliseconds to a Date object, which is what the second line does. The first line is just a way of getting a sensible number of milliseconds. You could also just do var date = new Date (0);. – Brian Donovan Aug 21 '13 at 17:49.

How to include milliseconds when converting datestr to datetime , convert a date string of the form 20-04-2018 09:37:46.657 ('dd-mm-yyyy HH:​MM:SS.FFF') to a datetime, but I cannot get the right input format. To extract the string representation of a time's millisecond component, call the date and time value's DateTime.ToString (String) or ToString method, and pass the fff or FFF custom format pattern either alone or with other custom format specifiers as the format parameter.

Convert date and time to string format - MATLAB datestr, DateString = datestr( t ) converts the datetime values in the input array t to text representing You can reformat the date and time, and also show milliseconds. Just enter the milliseconds value and press the Convert to Date button to find the date. You can also set the milliseconds value from Now button to the current timestamp milliseconds. For example 1471300214792 milliseconds is converted to date as 16 August 2016 01:30:14. Milliseconds Converters. Milliseconds to Microseconds; Milliseconds to Seconds

Docs, Calendar Recur Twitter Fiscal Quarters Parse Date Format Round Transform Instead of modifying the native Date.prototype , Moment.js creates a wrapper for the second, millisecond equal 0 means next day at midnight 20130208T080910 If you know the format of an input string, you can use that to parse a moment. The task is to write a program in Java to convert Milliseconds to a Date which Displays the date in dd MMM yyyy HH:mm:ss:SSS Z format. The Date class in Java internally stores Date in milliseconds. So any date is the number of milliseconds passed since January 1, 1970, 00:00:00 GMT and the Date class provides a constructor which can be used to