ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I need to get current time using Joda time format (http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html). Is there any way to get that???

 Ex : "EEE MMM dd HH:mm:ss.SSS yyyy"

Needed to convert into

 "Fri Mar 24 11:54:55.234 2017"

Replies are listed 'Best First'.
Re: Get current time using Joda time format
by haukex (Archbishop) on Mar 24, 2017 at 10:52 UTC
    "EEE MMM dd HH:mm:ss.SSS yyyy"

    That looks similar to the CLDR format that DateTime supports. However, I haven't fully compared that spec to the link you provided to see if they really are compatible, so you'd need to do that. If they aren't compatible, you may need to write your own format string converter, as I haven't found anything with a quick search on CPAN yet. This does seem to work:

    use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', time_zone=>'UTC', pattern => '%a %b %d %H:%M:%S.%3N %Y'); my $dt = $strp->parse_datetime('Fri Mar 24 11:54:55.234 2017'); print $dt->format_cldr('EEE MMM dd HH:mm:ss.SSS yyyy'), "\n"; __END__ Fri Mar 24 11:54:55.234 2017
Re: Get current time using Joda time format
by 1nickt (Canon) on Mar 24, 2017 at 12:04 UTC

    I need to get current time using Joda time format ... Ex : "EEE MMM dd HH:mm:ss.SSS yyyy"

    $ perl -Mstrict -Mwarnings -MDateTime -E' say DateTime->now->strftime("%a %b %d %T.%3N %Y") ' Fri Mar 24 11:53:41.000 2017
    $ perl -Mstrict -Mwarnings -MDateTime -E' say DateTime->now->format_cldr("EEE MMM dd HH:mm:ss.SSS yyyy") ' Fri Mar 24 11:54:35.000 2017
    Note that DateTime is apparently using time, so the milliseconds is always '000'. If you want more precision you'll have to get the hi-res time yourself using Time::HiRes, which you have as part of the Perl core:
    perl -Mstrict -Mwarnings -MTime::HiRes=time -MDateTime -E' say DateTime->from_epoch( epoch => time )->format_cldr("EEE MMM dd HH: +mm:ss.SSS yyyy") ' Fri Mar 24 11:55:26.137 2017

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Get current time using Joda time format
by Anonymous Monk on Mar 24, 2017 at 07:12 UTC

      Shouldn't it be strftime instead of strptime? I get the impression that ravi45722 has the format string and wants to produce the current date+time according to that format.

      But the modules work in both directions, resp. DateTime has formatters, so the module suggestions are still good no matter the direction of conversion.

      my $t = Time::Piece->strptime("Friday 24rd Mar, 2017", "%A %drd %b, % +Y"); print $t->strftime("%a %b %d %H:%m:%s %Y");

      gives something like this

       "Fri Mar 24 11:54:55.234 2017"

      But I need that as print

       $t->strftime("EEE MMM dd HH:mm:ss.SSS yyyy");

      Is there any module like that????

      I dont have unix format. I have only joda time format string

        It isn't clear to me how your problem hasn't been solved here, sorry. Perhaps you could re-state it in the form of a test so that everyone can see the issue? See How to ask better questions using Test::More and sample data for how to present that. Once we can understand what you are asking we will be better positioned to suggest some solution.

        Update: A thought: is it the case that you aren't interested in formatting the time but actually you want to re-format the format? ie. convert from Joda format specifiers to strftime specifiers?

Re: Get current time using Joda time format
by hippo (Archbishop) on Mar 24, 2017 at 09:34 UTC
    Is there any way to get that???

    Of course. There are many, many date and time manipulation modules. I would start with Time::Piece because (a) I find it easy to use and (b) it is a core module so it should already be installed with perl (or at worst, easily obtained). If you don't really need the milliseconds you can use the ctime method, but you can fall back on the strftime method to format it how you like.