You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需AnyLogic内置时间函数,如何修改日期显示格式?

在AnyLogic中不依赖内置时间函数格式化日期

当然可以不依赖AnyLogic的内置时间函数实现你要的日期格式,直接用Java原生的日期处理API就能搞定——毕竟AnyLogic底层基于Java,完全兼容Java的日期工具类。

具体实现方案

  • 先导入必要的Java日期类(若AnyLogic未自动导入):
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.ZoneOffset;
    
  • 创建指定格式的格式化器:
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E dd.MM.yyyy HH:mm");
    
  • 将AnyLogic模型时间转换为Java的LocalDateTime并格式化:
    // 以模型当前时间为例
    LocalDateTime currentModelTime = LocalDateTime.ofEpochSecond((long)time(), 0, ZoneOffset.UTC);
    String formattedDate = currentModelTime.format(formatter);
    
    执行后formattedDate会输出类似Mon 15.07.2024 14:30的格式。

额外优化说明

  • 若需要本地化的星期名称(比如中文的"周一"),可以给格式化器指定地区:
    import java.util.Locale;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E dd.MM.yyyy HH:mm", Locale.CHINA);
    
  • 如果你处理的是AnyLogic中的Date对象(如实验开始时间getExperimentStartDate()),可直接转换格式化:
    import java.time.ZoneId;
    LocalDateTime dateTime = yourDateObject.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    String formattedDate = dateTime.format(formatter);
    

内容的提问来源于stack exchange,提问作者Steffen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 16:32:31