Skip to content
Advertisement

Convert date in string(Mon Jan 16:20:12 India Standard Time 2014) to java.util.date in JAVA

I want to convert this (Mon Jan 16:20:12 India Standard Time 2014) Date in string to java.util.Date in java how can I do it. I want the date in the following format.

SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);

Actually the Date (Mon Jan 16:20:12 India Standard Time 2014) is argument from MFC application which will be launching a executable jar file and the date in string will be its argument. MFC Code that will launch the executable jar. with argument.

CString csCurrentTime = CTime::GetCurrentTime().Format("%a %b %X %Z %Y");
    TRACE(csCurrentTime);
    if (CreateProcess(m_csJrePath, TEXT(" -jar DbxUpldDwnld.jar %s",csCurrentTime), NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, (LPSTARTUPINFOA)&siStartupInfo, &piProcessInfo) == false) {
        AfxMessageBox(_T("Please install Java Runtime Environment(JRE) on your PCn Or JRE not found on given path in INI File."), MB_ICONERROR);
        CloseHandle(piProcessInfo.hProcess);
        CloseHandle(piProcessInfo.hThread);
        return;
    }

Also will it be possible to get the date with spaces as single argument in java. Please Help me, am new to MFC.

Advertisement

Answer

You need to use another SimpleDateFormat that has the format of your time-string to do the parsing. For your example, this one should work:

String time = "Mon Jan 16:20:12 India Standard Time 2014";
SimpleDateFormat parser = new SimpleDateFormat("E MMM HH:mm:ss z yyyy", Locale.US);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
System.out.println(formatter.format(parser.parse(time)));

As @TimB points out, all the date and time patterns can be found in the JavaDoc for SimpleDateFormat.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement