15

20200202,千年难遇啊!

 4 years ago
source link: https://segmentfault.com/a/1190000021672998
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

j6BJ3mA.png!web

本文收录在Java技术栈Github,欢迎Star:

https://github.com/javastacks/javastack

已经在家好几天没出门了,是不是都憋疯了?今天又是个特殊的日子,我来给大家分享一个节日小知识。

20200202 为什么说是千万难遇的一天?

因为今天是:世界完全对称日,很少遇见,看起来和回文数差不多,如:

2020 倒过来就是 0202,拼在一起就是:20200202

这个节日是否存在科学意义还无从考证,但却是十分有意思的一天,一千年也就那么十来个。

今天是农历正月初九,被大家寓意为长长久久,又因 "2020" 谐音 "爱你爱你",又被不少人称之为千年一遇的 "最佳领证日"!

这不,很多新人们都相继预约在 20200202 这天结婚领证,年前就已经排满了,但最近些天,不巧,这个事吧,我估计悬了。。

既然世界完全对称日很少见,那都有哪些呢?

下面,栈长用 Java 程序打出了 21 世纪所有的世界完全对称日,也就 12 个。

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author 栈长
 * @from 微信公众号:Java技术栈
 */
public class SymmetryDay {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        for (int y = 2000; y < 2100; y++) {
            String year = String.valueOf(y);
            calendar.set(y, getValue(year, 2) - 1, getValue(year, 0));
            String date = sdf.format(calendar.getTime());
            if (date.equals(year + new StringBuilder(year).reverse())) {
                System.out.println(date);
            }
        }
    }

    private static int getValue(String year, int begin) {
        return Integer.parseInt(new StringBuilder(year.substring(begin, begin + 2)).reverse().toString());
    }

}

输出结果:

下一个世界完全对称日是:20211202。

栈长用了一个很简单的反序对比操作来输出,个人感觉可能不是最佳的编程方式,你还会哪些比较骚的方法?欢迎留言分享。

关注 Java技术栈 微信公众号,栈长将继续分享好玩的 Java 技术,公众号第一时间推送。

AZZb6vn.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK