Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Get Email Date/Time

Demonstrates getting the email "Date" header field in a CkDateTime object.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.DtObj;
import com.chilkatsoft.chilkat2.Email;

public class ChilkatExample {

    public static void main(String[] args) {
        Email email = new Email();

        // Load a .eml file into the email object.
        email.loadEml("/home/users/chilkat/eml/myEml.eml");

        DateTime dtTime = new DateTime();
        dtTime.setFromRfc822(email.getEmailDateStr());

        // Once we have the CkDateTime object, we can get the date/time in many different formats:

        // Get as a RFC822 GMT string:
        boolean bLocalTime = false;
        System.out.println(dtTime.getAsRfc822(bLocalTime));

        // Get as an RFC822 string in the local timezone.
        // (remember, the daylight savings that existed at the given time in the past is applied)
        bLocalTime = true;
        System.out.println(dtTime.getAsRfc822(bLocalTime));

        // Get as a 32-bit UNIX time (local or GMT..)
        // The Unix time is number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
        int unixTime = (int) dtTime.getAsUnixTime(bLocalTime);
        System.out.println("Unix time: " + unixTime);

        // One can also get the as a "DtObj" object for accessing the individual
        // parts of the date/time, such as month, day, year, hour, minute, etc.
        // The DtObj can be obtained in the GMT or local timezone:
        DtObj dtObj = new DtObj();
        dtTime.toDtObj(bLocalTime, dtObj);

        if (!dtTime.getLastMethodSuccess()) {
            System.out.println("This should never really happen!");
            return;
        }

        System.out.println(dtObj.getDay() + "-" + dtObj.getMonth() + "-" + dtObj.getYear() + " " + dtObj.getHour() + ":" + dtObj.getMinute() + ":" + dtObj.getSecond());
    }
}